Created
June 20, 2013 14:51
-
-
Save 19h/5823413 to your computer and use it in GitHub Desktop.
AES CBC 256 using Node.js [crypto]
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
| /* | |
| p is plaintext (non-encrypted value) OR CIPHER (already encrypted text) | |
| t is either false [cbc256("value", false, "key")] for encryption or true for decryption | |
| k is key. | |
| */ | |
| /* | |
| Example: | |
| val = "value"; | |
| v = cbc256(val, false, "key"); | |
| v_ = cbc256(v, true, "key") | |
| // val == v_ | |
| */ | |
| cbc256 = function (p, t, k) { | |
| t = t || 0; | |
| crypto = this["crypto"] || require("crypto"); | |
| z = ""; | |
| if (!t) { | |
| _c = crypto.createCipher("aes-256-cbc", k); | |
| return _c.update(p, "utf8", "hex"), _c["final"]("hex"); | |
| } else { | |
| _c = crypto.createDecipher("aes-256-cbc", k); | |
| return _c.update(p, "hex", "utf8"), _c["final"]("utf8"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment