Skip to content

Instantly share code, notes, and snippets.

@mpneuried
Last active December 26, 2015 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpneuried/7215904 to your computer and use it in GitHub Desktop.
Save mpneuried/7215904 to your computer and use it in GitHub Desktop.
Simplify the data en/decryption in node.js
### USEAGE
crypto = require( "./crypto" )()
_crypted = crypto.crypt( "myPassowrd", "data123" )
crypto.decrypt( "myPassowrd", _crypted ) # data123
###
crypto = require( "crypto" )
module.exports = ( algorithm = "aes128", enc_in = "utf8", enc_out = "hex", padding = true )->
_ret =
crypt: ( key, str )->
cipher = crypto.createCipher( algorithm, new Buffer( key ) )
cipher.setAutoPadding( padding )
_cipher = cipher.update( str, enc_in, enc_out )
_cipher += cipher.final( enc_out )
return _cipher
decrypt: ( key, crypted )->
decipher = crypto.createDecipher( algorithm, new Buffer( key ) )
decipher.setAutoPadding( padding )
_decipher = decipher.update( crypted, enc_out, enc_in )
_decipher += decipher.final( enc_in )
return _decipher
return _ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment