-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
191 lines (149 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Node.js core modules
var crypto = require('crypto');
/**
* The encryption algorithm (cipher) type to be used.
* @type {String}
* @const
* @private
*/
var CIPHER_ALGORITHM = 'aes-256-ctr';
//
// Primary API
//
/**
* An API to allow for greatly simplified AES-256 encryption and decryption using a passphrase of
* any length plus a random Initialization Vector.
* @exports aes256
* @public
*/
var aes256 = {
/**
* Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
* @param {String} key A passphrase of any length to used to generate a symmetric session key.
* @param {String|Buffer} input The clear-text message or buffer to be encrypted.
* @returns {String|Buffer} A custom-encrypted version of the input.
* @public
* @method
*/
encrypt: function(key, input) {
if (typeof key !== 'string' || !key) {
throw new TypeError('Provided "key" must be a non-empty string');
}
var isString = typeof input === 'string';
var isBuffer = Buffer.isBuffer(input);
if (!(isString || isBuffer) || (isString && !input) || (isBuffer && !Buffer.byteLength(input))) {
throw new TypeError('Provided "input" must be a non-empty string or buffer');
}
var sha256 = crypto.createHash('sha256');
sha256.update(key);
// Initialization Vector
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);
var buffer = input;
if (isString) {
buffer = Buffer.from(input);
}
var ciphertext = cipher.update(buffer);
var encrypted = Buffer.concat([iv, ciphertext, cipher.final()]);
if (isString) {
encrypted = encrypted.toString('base64');
}
return encrypted;
},
/**
* Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
* @param {String} key A passphrase of any length to used to generate a symmetric session key.
* @param {String|Buffer} encrypted The encrypted message to be decrypted.
* @returns {String|Buffer} The original plain-text message or buffer.
* @public
* @method
*/
decrypt: function(key, encrypted) {
if (typeof key !== 'string' || !key) {
throw new TypeError('Provided "key" must be a non-empty string');
}
var isString = typeof encrypted === 'string';
var isBuffer = Buffer.isBuffer(encrypted);
if (!(isString || isBuffer) || (isString && !encrypted) || (isBuffer && !Buffer.byteLength(encrypted))) {
throw new TypeError('Provided "encrypted" must be a non-empty string or buffer');
}
var sha256 = crypto.createHash('sha256');
sha256.update(key);
var input = encrypted;
if (isString) {
input = Buffer.from(encrypted, 'base64');
if (input.length < 17) {
throw new TypeError('Provided "encrypted" must decrypt to a non-empty string or buffer');
}
} else {
if (Buffer.byteLength(encrypted) < 17) {
throw new TypeError('Provided "encrypted" must decrypt to a non-empty string or buffer');
}
}
// Initialization Vector
var iv = input.slice(0, 16);
var decipher = crypto.createDecipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);
var ciphertext = input.slice(16);
var output;
if (isString) {
output = decipher.update(ciphertext) + decipher.final();
} else {
output = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
}
return output;
}
};
/**
* Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
* @param {String} key A passphrase of any length to used to generate a symmetric session key.
* @public
* @constructor
*/
function AesCipher(key) {
if (typeof key !== 'string' || !key) {
throw new TypeError('Provided "key" must be a non-empty string');
}
/**
* A passphrase of any length to used to generate a symmetric session key.
* @member {String} key
* @readonly
*/
Object.defineProperty(this, 'key', { value: key });
}
/**
* Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
* @param {String} plaintext The clear-text message to be encrypted.
* @returns {String} A custom-encrypted version of the input.
* @public
* @method
*/
AesCipher.prototype.encrypt = function(plaintext) {
return aes256.encrypt(this.key, plaintext);
};
/**
* Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
* @param {String} encrypted The encrypted message to be decrypted.
* @returns {String} The original plain-text message.
* @public
* @method
*/
AesCipher.prototype.decrypt = function(encrypted) {
return aes256.decrypt(this.key, encrypted);
};
//
// API Extension
//
/**
* Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
* @param {String} key A passphrase of any length to used to generate a symmetric session key.
* @returns {AesCipher}
* @public
* @method
*/
aes256.createCipher = function(key) {
return new AesCipher(key);
};
//
// Export the API
//
module.exports = aes256;