-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
245 lines (223 loc) · 6.87 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
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
/* eslint-disable capitalized-comments,complexity,prefer-destructuring */
'use strict';
const scrypt = require('scrypt');
const tsse = require('tsse');
const phc = require('@phc/format');
const gensalt = require('@kdf/salt');
const MAX_UINT32 = 4294967295; // 2**32 - 1
/**
* Default configurations used to generate a new hash.
* @private
* @type {Object}
*/
const defaults = Object.freeze({
// CPU/memory cost (N), in number of sequential hash results stored in RAM by the ROMix function.
cost: 15, // 2^15
// blocksize (r), in bits used by the BlockMix function.
blocksize: 8,
// parallelism (p), in number of instances of the mixing function run independently.
parallelism: 1,
// The minimum recommended size for the salt is 128 bits.
saltSize: 16
});
/**
* Computes the hash string of the given password in the PHC format using scrypt
* package.
* @public
* @param {string} password The password to hash.
* @param {Object} [options] Optional configurations related to the hashing
* function.
* @param {number} [options.blocksize=8] Optional amount of memory to use in
* kibibytes.
* Must be an integer within the range (`8` <= `memory` <= `2^32-1`).
* @param {number} [options.cost=15] Optional CPU/memory cost parameter.
* Must be an integer power of 2 within the range
* (`2` <= `cost` <= `2^((128 * blocksize) / 8) - 1`).
* @param {number} [options.parallelism=1] Optional degree of parallelism to
* use.
* Must be an integer within the range
* (`1` <= `parallelism` <= `((2^32-1) * 32) / (128 * blocksize)`).
* @return {Promise.<string>} The generated secure hash string in the PHC
* format.
*/
function hash(password, options) {
options = options || {};
const blocksize = options.blocksize || defaults.blocksize;
const cost = options.cost || defaults.cost;
const parallelism = options.parallelism || defaults.parallelism;
const saltSize = options.saltSize || defaults.saltSize;
// Blocksize Validation
if (typeof blocksize !== 'number' || !Number.isInteger(blocksize)) {
return Promise.reject(
new TypeError("The 'blocksize' option must be an integer")
);
}
if (blocksize < 1 || blocksize > MAX_UINT32) {
return Promise.reject(
new TypeError(
`The 'blocksize' option must be in the range (1 <= blocksize <= ${MAX_UINT32})`
)
);
}
// Cost Validation
if (typeof cost !== 'number' || !Number.isInteger(cost)) {
return Promise.reject(
new TypeError("The 'cost' option must be an integer")
);
}
const maxcost = (128 * blocksize) / 8 - 1;
if (cost < 2 || cost > maxcost) {
return Promise.reject(
new TypeError(
`The 'cost' option must be in the range (1 <= cost <= ${maxcost})`
)
);
}
// Parallelism Validation
if (typeof parallelism !== 'number' || !Number.isInteger(parallelism)) {
return Promise.reject(
new TypeError("The 'parallelism' option must be an integer")
);
}
const maxpar = Math.floor(((Math.pow(2, 32) - 1) * 32) / (128 * blocksize));
if (parallelism < 1 || parallelism > maxpar) {
return Promise.reject(
new TypeError(
`The 'parallelism' option must be in the range (1 <= parallelism <= ${maxpar})`
)
);
}
// Salt Size Validation
if (saltSize < 8 || saltSize > 1024) {
return Promise.reject(
new TypeError(
"The 'saltSize' option must be in the range (8 <= saltSize <= 1023)"
)
);
}
const params = {
N: Math.pow(2, cost),
r: blocksize,
p: parallelism
};
const keylen = 32;
return gensalt(saltSize).then(salt => {
return scrypt.hash(password, params, keylen, salt).then(hash => {
const phcstr = phc.serialize({
id: 'scrypt',
params: {
ln: cost,
r: blocksize,
p: parallelism
},
salt,
hash
});
return phcstr;
});
});
}
/**
* Determines whether or not the hash stored inside the PHC formatted string
* matches the hash generated for the password provided.
* @public
* @param {string} phcstr Secure hash string generated from this package.
* @param {string} password User's password input.
* @returns {Promise.<boolean>} A boolean that is true if the hash computed
* for the password matches.
*/
function verify(phcstr, password) {
let phcobj;
try {
phcobj = phc.deserialize(phcstr);
} catch (err) {
return Promise.reject(err);
}
// Identifier Validation
if (phcobj.id !== 'scrypt') {
return Promise.reject(
new TypeError(`Incompatible ${phcobj.id} identifier found in the hash`)
);
}
// Parameters Existence Validation
if (typeof phcobj.params !== 'object') {
return Promise.reject(new TypeError('The param section cannot be empty'));
}
// Blocksize Validation
if (
typeof phcobj.params.r !== 'number' ||
!Number.isInteger(phcobj.params.r)
) {
return Promise.reject(new TypeError("The 'r' param must be an integer"));
}
if (phcobj.params.r < 1 || phcobj.params.r > MAX_UINT32) {
return Promise.reject(
new TypeError(
`The 'r' param must be in the range (1 <= r <= ${MAX_UINT32})`
)
);
}
// Cost Validation
if (
typeof phcobj.params.ln !== 'number' ||
!Number.isInteger(phcobj.params.ln)
) {
return Promise.reject(new TypeError("The 'ln' param must be an integer"));
}
const maxcost = (128 * phcobj.params.r) / 8 - 1;
if (phcobj.params.ln < 1 || phcobj.params.ln > maxcost) {
return Promise.reject(
new TypeError(
`The 'ln' param must be in the range (1 <= ln <= ${maxcost})`
)
);
}
// Parallelism Validation
if (
typeof phcobj.params.p !== 'number' ||
!Number.isInteger(phcobj.params.p)
) {
return Promise.reject(new TypeError("The 'p' param must be an integer"));
}
const maxpar = Math.floor(
((Math.pow(2, 32) - 1) * 32) / (128 * phcobj.params.p)
);
if (phcobj.params.p < 1 || phcobj.params.p > maxpar) {
return Promise.reject(
new TypeError(`The 'p' param must be in the range (1 <= p <= ${maxpar})`)
);
}
const params = {
N: Math.pow(2, phcobj.params.ln),
r: phcobj.params.r,
p: phcobj.params.p
};
// Salt Validation
if (typeof phcobj.salt === 'undefined') {
return Promise.reject(new TypeError('No salt found in the given string'));
}
const salt = phcobj.salt;
// Hash Validation
if (typeof phcobj.hash === 'undefined') {
return Promise.reject(new TypeError('No hash found in the given string'));
}
const hash = phcobj.hash;
const keylen = phcobj.hash.byteLength;
return scrypt.hash(password, params, keylen, salt).then(newhash => {
const match = tsse(hash, newhash);
return match;
});
}
/**
* Gets the list of all identifiers supported by this hashing function.
* @public
* @returns {string[]} A list of identifiers supported by this hashing function.
*/
function identifiers() {
return ['scrypt'];
}
module.exports = {
hash,
verify,
identifiers
};