Created
November 6, 2014 02:29
-
-
Save ccorcos/1a26653ac5b59cdc3a59 to your computer and use it in GitHub Desktop.
AES symmetric encryption crypto node.js
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
crypto = require "crypto" | |
key = "this is my password. Make it as long as you want. But its hashed into 512 bytes." | |
text = "This is the text. as long as you want it" | |
hash = (utf8String)-> | |
hasher = crypto.createHash('sha512') | |
hasher.update(utf8String, 'utf8') | |
hexString = hasher.digest('hex') | |
return hexString | |
encrypt = (utf8String, key) -> | |
cipher = crypto.createCipher('aes-256-cbc', key) | |
encryptedData = cipher.update(utf8String, 'utf8', 'hex') | |
encryptedData += cipher.final('hex') | |
return encryptedData | |
decrypt = (hexString, key) -> | |
decipher = crypto.createDecipher('aes-256-cbc', key) | |
decryptedData = decipher.update(hexString, 'hex', 'utf8') | |
decryptedData += decipher.final('utf8') | |
return decryptedData | |
console.log "key:", key | |
console.log "text:", text | |
hashKey = hash(key) | |
console.log "hashed key:", hashKey | |
encrypted = encrypt(text, hashKey) | |
console.log "encrypted:", encrypted | |
decrypted = decrypt(encrypted, hashKey) | |
console.log "decrypted:", decrypted | |
console.log "did it work?", decrypted == text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment