Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. "use strict";
  2. var crypto = require("crypto");
  3.  
  4. var EncryptionHelper = (function () {
  5.  
  6. function getKeyAndIV(key, callback) {
  7.  
  8. crypto.pseudoRandomBytes(16, function (err, ivBuffer) {
  9.  
  10. var keyBuffer = (key instanceof Buffer) ? key : new Buffer(key) ;
  11.  
  12. callback({
  13. iv: ivBuffer,
  14. key: keyBuffer
  15. });
  16. });
  17. }
  18.  
  19. function encryptText(cipher_alg, key, iv, text, encoding) {
  20.  
  21. var cipher = crypto.createCipheriv(cipher_alg, key, iv);
  22.  
  23. encoding = encoding || "binary";
  24.  
  25. var result = cipher.update(text, "utf8", encoding);
  26. result += cipher.final(encoding);
  27.  
  28. return result;
  29. }
  30.  
  31. function decryptText(cipher_alg, key, iv, text, encoding) {
  32.  
  33. var decipher = crypto.createDecipheriv(cipher_alg, key, iv);
  34.  
  35. encoding = encoding || "binary";
  36.  
  37. var result = decipher.update(text, encoding);
  38. result += decipher.final();
  39.  
  40. return result;
  41. }
  42.  
  43. return {
  44. CIPHERS: {
  45. "AES_128": "aes128", //requires 16 byte key
  46. "AES_128_CBC": "aes-128-cbc", //requires 16 byte key
  47. "AES_192": "aes192", //requires 24 byte key
  48. "AES_256": "aes256" //requires 32 byte key
  49. },
  50. getKeyAndIV: getKeyAndIV,
  51. encryptText: encryptText,
  52. decryptText: decryptText
  53. };
  54. })();
  55.  
  56. module.exports = EncryptionHelper;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement