Created
May 12, 2015 09:46
-
-
Save eduardostalinho/f5121bd990f9452efabf to your computer and use it in GitHub Desktop.
Generate and encrypt and decrypt konami codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var crypto = require('crypto'), | |
| algorithm = 'aes-256-gcm', | |
| password = 'my_precious', | |
| // do not use a global iv for production, | |
| // generate a new one for each encryption | |
| iv = '6891jhiyfaskdfa'; | |
| var defaultLength = 10 | |
| var makePattern = function(length) { | |
| if (!length) { | |
| length = defaultLength | |
| } | |
| var keyCodes = [ | |
| '37', '38', '39', '40', '65', '66', '76', '82', '88', '89' | |
| ]; | |
| var keySigns = [ | |
| '←', '↑', '→', '↓', 'A', 'B', 'L', 'R', 'X', 'Y' | |
| ]; | |
| var pattern = []; | |
| for(var i=0; i < length; i++ ) { | |
| pattern.push( | |
| keyCodes[Math.floor(Math.random() * keyCodes.length)] | |
| ); | |
| } | |
| var patternSigns = pattern.map(function (c) { | |
| return keySigns[keyCodes.indexOf(c)] | |
| }); | |
| return [ | |
| patternSigns.join().replace(/,/g, ' '), | |
| pattern.join().replace(/,/g, '') | |
| ]; | |
| } | |
| // Nodejs encryption with GCM | |
| // Does not work with nodejs v0.10.31 | |
| // Part of https://github.com/chris-rock/node-crypto-examples | |
| function encrypt(text) { | |
| var cipher = crypto.createCipheriv(algorithm, password, iv) | |
| var encrypted = cipher.update(text, 'utf8', 'hex') | |
| encrypted += cipher.final('hex'); | |
| var tag = cipher.getAuthTag(); | |
| return { | |
| content: encrypted, | |
| tag: tag | |
| }; | |
| } | |
| function decrypt(encrypted) { | |
| var decipher = crypto.createDecipheriv(algorithm, password, iv) | |
| decipher.setAuthTag(encrypted.tag); | |
| var dec = decipher.update(encrypted.content, 'hex', 'utf8') | |
| dec += decipher.final('utf8'); | |
| return dec; | |
| } | |
| var p = makePattern() | |
| console.log(p[0] + '\n' + p[1]) | |
| var hw = encrypt(p[1]) | |
| console.log(hw) | |
| console.log(decrypt(hw)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment