-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrypt.js
57 lines (45 loc) · 1.94 KB
/
scrypt.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
const crypto = require('crypto');
const buffer = require('buffer');
const _ = require('lodash');
module.exports = function verifyPassword (password, hashSettings, callback) {
const hashValue = _.get(hashSettings, 'hash.value');
const hashEncoding = _.get(hashSettings, 'hash.encoding', 'base64');
const hashBuffer = Buffer.from(hashValue, hashEncoding);
const saltValue = _.get(hashSettings, 'salt.value');
const saltEncoding = _.get(hashSettings, 'salt.encoding', 'ascii');
const saltBuffer = Buffer.from(saltValue, saltEncoding);
const costFactor = _.get(hashSettings, 'costfactor');
const blockSize = _.get(hashSettings, 'blocksize');
const parallelization = _.get(hashSettings, 'parallelization');
const keyLength = _.get(hashSettings, 'keylength');
const passwordEncoding = _.get(hashSettings, 'password.encoding', 'utf16le');
const passwordBuffer = Buffer.from(password, passwordEncoding);
const params = {
'N': costFactor,
'r': blockSize,
'p': parallelization,
'maxmem': costFactor * blockSize * 256
}
crypto.scrypt(passwordBuffer, saltBuffer, keyLength, params, (err, calculatedHashBuffer) => {
if (err) {
callback(err);
}
console.log("Current hash " , hashBuffer.toString(hashEncoding));
console.log("Calculated hash " , calculatedHashBuffer.toString(hashEncoding));
try {
const isValidPassword = crypto.timingSafeEqual(
calculatedHashBuffer,
hashBuffer,
);
console.log("isValidPassword" , isValidPassword);
callback(null, isValidPassword);
} catch (err) {
const message = _.get(err, 'message', '').toLowerCase();
if (_.includes(message, 'buffers must have the same length')) {
callback(null, false);
} else {
callback(err);
}
}
});
}