-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptography.js
456 lines (427 loc) · 17.4 KB
/
cryptography.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import crypto from 'crypto';
import fs from 'fs';
/**
* Provides a straight-forward set of encryption/decryption functions for encrypting content.
* A limited subset of algorithms are supported.
*/
class Cryptography {
constructor(algorithm, hashAlgorithm, encoding, outputEncoding) {
/**
* The algorithm to use.
* Supports:
* - aes-128-ctr
* - aes-192-ctr
* - aes-256-ctr
* Defaults to "aes-256-ctr".
* @type {String}
*/
this.algorithm = algorithm || 'aes-256-ctr';
/**
* The hasing algorithm to use.
* Supports:
* - ripemd160
* - sha256
* - sha384
* - sha512
* Defaults to "sha256".
* @type {String}
*/
this.hashAlgorithm = hashAlgorithm || 'sha256';
/**
* The text encoding. Defaults to "utf8".
* @type {String}
*/
this.encoding = encoding || 'utf8';
/**
* The encrypted text output encoding. Can be 'base64' or 'hex'.
* @type {String}
*/
this.outputEncoding = outputEncoding || 'base64';
this.pbkdf2 = {
iterations: 100
};
this._validateProperties();
}
/**
* Generates a public and private key for the given algorithm.
* @param {String} algorithm - Can be 'rsa’, ‘dsa’, ‘ec’, ‘ed25519’, ‘ed448’, ‘x25519’, ‘x448’, or ‘dh’.
* @returns {crypto.KeyPairSyncResult.<String, String> | crypto.KeyPairKeyObjectResult}
*/
static keypair(algorithm) {
let pair = crypto.generateKeyPairSync(algorithm);
return pair;
}
/**
* Converts a hexidecimal string containing a private key into a key object.
* @param {String} pkValue - The hexidecimal private key value.
* @param {crypto.KeyType} type - The private key type.
* @param {crypto.KeyFormat} format - The private key format.
* @returns {crypto.KeyObject}
*/
static privateKeyFromString(pkValue, type, format) {
let token = Buffer.from(pkValue, 'hex');
return crypto.createPrivateKey({
key: token,
format: format,
type: type
});
}
/**
* Converts a hexidecimal string containing a public key into a key object.
* @param {String} pubValue - The hexidecimal public key value.
* @param {crypto.KeyType} type - The private key type.
* @param {crypto.KeyFormat} format - The private key format.
* @returns {crypto.KeyObject}
*/
static publicKeyFromString(pubValue, type, format) {
let token = Buffer.from(pubValue, 'hex');
return crypto.createPublicKey({
key: token,
format: format,
type: type
});
}
/**
* Generates the public key from a given private key.
* @param {crypto.KeyObject} pk - The private key.
* @param {crypto.KeyType} type - The private key type.
* @param {crypto.KeyFormat} format - The private key format.
* @returns {crypto.KeyObject}
*/
static publicKeyFromPrivateKey(pk, type, format) {
return crypto.createPublicKey({
key: pk,
format: format,
type: type
});
}
/**
* Validates the class properties.
* @private
*/
_validateProperties() {
if (!this.algorithm || !this.algorithm.match(/aes-(?:128|192|256)-ctr/)) {
throw new Error(`Invalid or un-supported algorithm value: "${this.algorithm}".`);
}
if (!this.hashAlgorithm || !this.hashAlgorithm.match(/ripemd160|sha256|sha384|sha512|whirlpool/)) {
throw new Error(`Invalid or un-supported hash algorithm value: "${this.hashAlgorithm}".`);
}
if (!this.encoding || !this.encoding.match(/utf8|ascii/)) {
throw new Error(`Invalid or un-supported encoding value: "${this.encoding}".`);
}
if (!this.outputEncoding || !this.outputEncoding.match(/base64|hex/)) {
throw new Error(`Invalid or un-supported outputEncoding value: "${this.outputEncoding}".`);
}
}
/**
* Returns the key size of the specified algorithm.
* @param {String} algorithm - The cryptographic algorithm to get the key size for.
* @returns {Number}
* @private
*/
_keyLength(algorithm) {
switch (algorithm) {
case 'aes-128-ctr': return 16;
case 'aes-192-ctr': return 24;
case 'aes-256-ctr': return 32;
}
return -1;
}
/**
* Creates a pbkdf2 derived key from the given password and salt.
* @param {String} password - The password to derive a key from.
* @param {String} salt - A random salt value.
* @returns {Promise.<Buffer>}
*/
async pbkdf2Key(password, salt) {
let self = this;
let key = {
data: null,
salt: Buffer.from(salt || '', self.encoding)
};
return new Promise((resolve, reject) => {
crypto.pbkdf2(
password,
key.salt,
self.pbkdf2.iterations,
self._keyLength(self.algorithm),
self.hashAlgorithm,
(err, derivedKey) => {
if (err) {
reject(err);
} else {
key.data = derivedKey;
resolve(key);
}
});
});
}
/**
* Encrypts a string of text using the given password and salt.
* @param {String} text - The string of text to encrypt.
* @param {String|{data:Buffer, salt:Buffer}} password - The password to derive a key from.
* @param {String} [salt] - A random salt value.
* @returns {Promise.<String>}
*/
async encryptText(text, password, salt) {
if (text === null) {
return null;
}
let encrypted = await this.encryptBuffer(Buffer.from(text, this.encoding), password, salt);
return encrypted.toString(this.outputEncoding);
}
/**
* Decrypts a string of text using the given password and salt.
* @param {String} text - The string of text to decrypt.
* @param {String|{data:Buffer, salt:Buffer}} password - The password to derive a key from.
* @param {String} [salt] - A random salt value.
* @returns {Promise.<String>}
*/
async decryptText(text, password, salt) {
if (text === null) {
return null;
}
let decrypted = await this.decryptBuffer(Buffer.from(text, this.outputEncoding), password, salt);
return decrypted.toString(this.encoding);
}
/**
* Encrypts a buffer using the given password and salt.
* @param {Buffer} buffer - The buffer to encrypt.
* @param {String|{data:Buffer, salt:Buffer}} password - The password to derive a key from.
* @param {String} [salt] - A random salt value.
* @returns {Promise.<Buffer>}
*/
async encryptBuffer(buffer, password, salt) {
//validations & escapes
if (buffer === null) {
return null;
} else if (Buffer.isBuffer(buffer) === false) {
throw new Error('The "buffer" parameter argument must be a Buffer instance.');
}
if (!password) {
throw new Error('A "password" parameter argument is required.');
} else if (!password.data && !password.salt && typeof password !== 'string') {
throw new Error('A "password" parameter argument must be a string or pbkdf2 derived key.');
}
if (salt && typeof salt !== 'string') {
throw new Error('The "salt" parameter argument must be a string if specified.');
}
this._validateProperties();
//encrypt the data
let key = null;
if (password.data && password.salt) {
key = password;
if (typeof salt !== 'undefined') {
throw new Error('The "salt" parameter cannot be used when providing a pbkdf2 derived key.');
}
} else {
key = await this.pbkdf2Key(password, salt);
}
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv(this.algorithm, key.data, iv);
var encrypted = cipher.update(buffer);
return Buffer.concat([iv, encrypted, cipher.final()]);
}
/**
* Decrypts a buffer using the given password and salt.
* @param {Buffer} buffer - The buffer to decrypt.
* @param {String|{data:Buffer, salt:Buffer}} password - The password to derive a key from.
* @param {String} [salt] - A random salt value.
* @returns {Promise.<Buffer>}
*/
async decryptBuffer(buffer, password, salt) {
//validations & escapes
if (buffer === null) {
return null;
} else if (Buffer.isBuffer(buffer) === false) {
throw new Error('The "buffer" parameter argument must be a Buffer instance.');
}
if (!password) {
throw new Error('A "password" paramater argument is required.');
} else if (!password.data && !password.salt && typeof password !== 'string') {
throw new Error('A "password" paramater argument must be a string or pbkdf2 derived key.');
}
if (salt && typeof salt !== 'string') {
throw new Error('The "salt" parameter argument must be a string if specified.');
}
this._validateProperties();
//decrypt the data
let key = null;
if (password.data && password.salt) {
key = password;
if (typeof salt !== 'undefined') {
throw new Error('The "salt" parameter cannot be used when providing a pbkdf2 derived key.');
}
} else {
key = await this.pbkdf2Key(password, salt);
}
let iv = buffer.slice(0, 16);
let encrypted = buffer.slice(16);
var decipher = crypto.createCipheriv(this.algorithm, key.data, iv);
var decrypted = decipher.update(encrypted);
return Buffer.concat([decrypted, decipher.final()]);
}
/**
* Creates a message digest of the input text using the current hashing algorithm.
* @param {String} text - The text to digest.
* @param {String} [salt] - A random salt value.
* @returns {String}
*/
hashText(text, salt) {
if (text === null) {
return null;
}
let digest = this.hashBuffer(Buffer.from(text, this.encoding), salt);
return digest.toString(this.outputEncoding);
}
/**
* Creates a message digest of the input buffer using the current hashing algorithm.
* @param {Buffer} buffer - The buffer to digest.
* @param {String} [salt] - A random salt value.
* @returns {Buffer}
*/
hashBuffer(buffer, salt) {
//validations & escapes
if (buffer === null) {
return null;
} else if (Buffer.isBuffer(buffer) === false) {
throw new Error('The "buffer" parameter argument must be a Buffer instance.');
}
if (salt && typeof salt !== 'string') {
throw new Error('The "salt" parameter argument must be a string if specified.');
}
this._validateProperties();
//create a message digest
let message = Buffer.concat([buffer, Buffer.from(salt || '', this.encoding)]);
let hash = crypto.createHash(this.hashAlgorithm);
return hash
.update(message)
.digest();
}
/**
* Creates a message digest of the input text using the current hashing algorithm.
* @param {String} filePath - The file path pointing to the file to hash.
* @param {String} [salt] - A random salt value.
* @returns {Promise.<Buffer>}
*/
async hashFile(filePath, salt) {
if (salt && typeof salt !== 'string') {
throw new Error('The "salt" parameter argument must be a string if specified.');
}
return new Promise((resolve, reject) => {
let hash = crypto.createHash(this.hashAlgorithm);
try {
let s = fs.ReadStream(filePath);
s.on('data', function (data) {
hash.update(data);
});
s.on('end', function () {
if (salt) {
hash.update(Buffer.from(salt || '', this.encoding));
}
return resolve(hash.digest());
});
} catch (error) {
return reject(error);
}
});
}
/**
* Returns the signature of text using the given secret as a key with the hash algorithm.
* @param {String} text - The text of data to sign.
* @param {String|Buffer} secretOrPrivateKey - The secret to used as a key for the signature generation. By default
* this is a shared secret, however, if `asymmetric` is `true` then it should represent a private key from a
* supported asymmetric algorithm (see #keypair function).
* @param {Boolean} [asymmetric=false] - Indicates a private key is provided and it's asymmetric algorithm should
* be used for signing.
* @returns {String}
*/
signText(text, secretOrPrivateKey, asymmetric) {
if (text === null) {
return null;
}
let sig = this.signBuffer(Buffer.from(text, this.encoding), secretOrPrivateKey, asymmetric);
return sig.toString(this.outputEncoding);
}
/**
* Verifies the signature of a given text and secret and returns a boolean indicating success.
* @param {String} text - The buffer of data that was signed.
* @param {String|Buffer} secretOrPublicKey - The secret to used as a key for the signature verification. By default
* this is a shared secret, however, if `asymmetric` is `true` then it should represent a *public* key from a
* supported asymmetric algorithm (see #keypair function).
* @param {String} signature - The signature expected.
* @param {Boolean} [asymmetric=false] - Indicates a public key is provided and it's asymmetric algorithm should
* be used for verification. Note that if the public key is invalid, a `false` is automatically returned.
* @returns {Boolean}
*/
verifyText(text, secretOrPublicKey, signature, asymmetric) {
if (text === null) {
return null;
}
return this.verifyBuffer(Buffer.from(text, this.encoding), secretOrPublicKey, Buffer.from(signature, this.outputEncoding), asymmetric);
}
/**
* Returns the signature of a buffer using the given secret as a key with the hash algorithm.
* @param {Buffer} buffer - The buffer of data to sign.
* @param {String|Buffer} secretOrPrivateKey - The secret to used as a key for the signature generation. By default
* this is a shared secret, however, if `asymmetric` is `true` then it should represent a private key from a
* supported asymmetric algorithm (see #keypair function).
* @param {Boolean} [asymmetric=false] - Indicates a private key is provided and it's asymmetric algorithm should
* be used for signing.
* @returns {Buffer}
*/
signBuffer(buffer, secretOrPrivateKey, asymmetric) {
if (asymmetric) {
return crypto.sign(null, buffer, secretOrPrivateKey);
}
return crypto.createHmac(this.hashAlgorithm, secretOrPrivateKey)
.update(buffer)
.digest();
}
/**
* Verifies the signature of a given buffer and secret and returns a boolean indicating success.
* @param {Buffer} buffer - The buffer of data that was signed.
* @param {String|Buffer} secretOrPublicKey - The secret to used as a key for the signature verification. By default
* this is a shared secret, however, if `asymmetric` is `true` then it should represent a *public* key from a
* supported asymmetric algorithm (see #keypair function).
* @param {Buffer} signature - The signature expected.
* @param {Boolean} [asymmetric=false] - Indicates a public key is provided and it's asymmetric algorithm should
* be used for verification. Note that if the public key is invalid, a `false` is automatically returned.
* @returns {Boolean}
*/
verifyBuffer(buffer, secretOrPublicKey, signature, asymmetric) {
if (asymmetric) {
try {
return crypto.verify(null, buffer, secretOrPublicKey, signature);
} catch (err) {
return false;
}
}
let sig = this.signBuffer(buffer, secretOrPublicKey);
if (sig.byteLength !== signature.byteLength) {
return false;
}
return crypto.timingSafeEqual(sig, signature);
}
/**
* Returns a random string of text at or between the specified min and max length.
* @param {Number} min - The minimum length of the string.
* @param {Number} max - The maximum length of the string.
* @returns {Buffer}
*/
randomText(min, max) {
let len = Math.floor(Math.random() * (max + 1 - min) + min);
return crypto.randomBytes(max).toString('base64').substr(0, len);
}
/**
* Returns a buffer of a random size at or between the specified min and max length, populated with random data.
* @param {Number} min - The minimum byte length of the buffer.
* @param {Number} max - The maximum byte length of the buffer.
* @returns {Buffer}
*/
randomBuffer(min, max) {
let len = Math.floor(Math.random() * (max + 1 - min) + min);
return crypto.randomBytes(len);
}
}
export default Cryptography;