|
2 | 2 |
|
3 | 3 | let crypto = require('crypto');
|
4 | 4 |
|
5 |
| -function UserException(message) { |
6 |
| - this.message = message; |
7 |
| - this.name = 'UserException'; |
8 |
| -} |
9 |
| - |
10 | 5 | try {
|
11 | 6 | var savedKeys = require("./config.js").k;
|
12 | 7 | } catch (e) {
|
13 |
| - throw new UserException('No Configuration Exists'); |
| 8 | + Promise.reject('No Configuration Exists!'); |
14 | 9 | }
|
15 | 10 |
|
16 | 11 | var ALGORITHM, KEY, HMAC_ALGORITHM, HMAC_KEY;
|
@@ -43,48 +38,47 @@ var constant_time_compare = function (val1, val2) {
|
43 | 38 |
|
44 | 39 | module.exports = {
|
45 | 40 |
|
46 |
| - "enc": { |
47 |
| - run : function (plain_text) { |
| 41 | + "encrypt": function (plain_text) { |
| 42 | + if (!plain_text || typeof(plain_text) !== "string") Promise.reject("Plain text not found."); |
48 | 43 |
|
49 |
| - var IV = Buffer.from(randomValueHex(16)); // ensure that the IV (initialization vector) is random |
50 |
| - var encryptor, cipher_text, hmac; |
| 44 | + var IV = Buffer.from(randomValueHex(16)); // ensure that the IV (initialization vector) is random |
| 45 | + var encryptor, cipher_text, hmac; |
51 | 46 |
|
52 |
| - encryptor = crypto.createCipheriv(ALGORITHM, KEY, IV); |
53 |
| - encryptor.setEncoding('hex'); |
54 |
| - encryptor.write(plain_text); |
55 |
| - encryptor.end(); |
| 47 | + encryptor = crypto.createCipheriv(ALGORITHM, KEY, IV); |
| 48 | + encryptor.setEncoding('hex'); |
| 49 | + encryptor.write(plain_text); |
| 50 | + encryptor.end(); |
56 | 51 |
|
57 |
| - cipher_text = encryptor.read(); |
| 52 | + cipher_text = encryptor.read(); |
58 | 53 |
|
59 |
| - hmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY); |
60 |
| - hmac.update(cipher_text); |
61 |
| - hmac.update(IV.toString('hex')); // ensure that both the IV and the cipher-text is protected by the HMAC |
| 54 | + hmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY); |
| 55 | + hmac.update(cipher_text); |
| 56 | + hmac.update(IV.toString('hex')); // ensure that both the IV and the cipher-text is protected by the HMAC |
62 | 57 |
|
63 |
| - // The IV isn't a secret so it can be stored along side everything else |
64 |
| - return cipher_text + "$" + IV.toString('hex') + "$" + hmac.digest('hex') |
65 |
| - } |
| 58 | + // The IV isn't a secret so it can be stored along side everything else |
| 59 | + return cipher_text + "$" + IV.toString('hex') + "$" + hmac.digest('hex') |
66 | 60 | },
|
67 | 61 |
|
68 |
| - "dec": { |
69 |
| - run : function (cipher_text) { |
70 |
| - var cipher_blob = cipher_text.split("$"); |
71 |
| - var ct = cipher_blob[0]; |
72 |
| - var IV = Buffer.from(cipher_blob[1], 'hex'); |
73 |
| - var hmac = cipher_blob[2]; |
74 |
| - var chmac, decryptor; |
75 |
| - |
76 |
| - chmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY); |
77 |
| - chmac.update(ct); |
78 |
| - chmac.update(IV.toString('hex')); |
79 |
| - |
80 |
| - if (!constant_time_compare(chmac.digest('hex'), hmac)) { |
81 |
| - Promise.reject("Encrypted Blob has been tampered with..."); |
82 |
| - return null; |
83 |
| - } |
84 |
| - |
85 |
| - decryptor = crypto.createDecipheriv(ALGORITHM, KEY, IV); |
86 |
| - var decryptedText = decryptor.update(ct, 'hex', 'utf-8'); |
87 |
| - return decryptedText + decryptor.final('utf-8'); |
| 62 | + "decrypt": function (cipher_text) { |
| 63 | + if (!cipher_text || typeof(cipher_text) !== "string" || !cipher_text.match("$")) Promise.reject("A valid cipher text not found."); |
| 64 | + |
| 65 | + var cipher_blob = cipher_text.split("$"); |
| 66 | + var ct = cipher_blob[0]; |
| 67 | + var IV = Buffer.from(cipher_blob[1], 'hex'); |
| 68 | + var hmac = cipher_blob[2]; |
| 69 | + var chmac, decryptor; |
| 70 | + |
| 71 | + chmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY); |
| 72 | + chmac.update(ct); |
| 73 | + chmac.update(IV.toString('hex')); |
| 74 | + |
| 75 | + if (!constant_time_compare(chmac.digest('hex'), hmac)) { |
| 76 | + Promise.reject("Encrypted Blob has been tampered with."); |
88 | 77 | }
|
| 78 | + |
| 79 | + decryptor = crypto.createDecipheriv(ALGORITHM, KEY, IV); |
| 80 | + var decryptedText = decryptor.update(ct, 'hex', 'utf-8'); |
| 81 | + return decryptedText + decryptor.final('utf-8'); |
89 | 82 | }
|
| 83 | + |
90 | 84 | }
|
0 commit comments