-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
296 lines (248 loc) · 7.34 KB
/
index.js
File metadata and controls
296 lines (248 loc) · 7.34 KB
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
/**
* This file is part of Hashy which is released under the MIT license.
*
* @author Julien Fontanet <julien.fontanet@isonoe.net>
*/
"use strict";
// ===================================================================
const promiseToolbox = require("promise-toolbox");
const asCallback = promiseToolbox.asCallback;
const promisifyAll = promiseToolbox.promisifyAll;
// ===================================================================
// Similar to Bluebird.method(fn) but handle Node callbacks.
const makeAsyncWrapper = (function (push) {
return function makeAsyncWrapper(fn) {
return function asyncWrapper() {
const args = [];
push.apply(args, arguments);
let callback;
const n = args.length;
if (n && typeof args[n - 1] === "function") {
callback = args.pop();
}
return asCallback.call(
new Promise(function (resolve) {
resolve(fn.apply(this, args));
}),
callback,
);
};
};
})(Array.prototype.push);
// ===================================================================
const algorithmsById = Object.create(null);
const algorithmsByName = Object.create(null);
const globalOptions = Object.create(null);
exports.options = globalOptions;
let DEFAULT_ALGO;
Object.defineProperty(exports, "DEFAULT_ALGO", {
enumerable: true,
get: function () {
return DEFAULT_ALGO;
},
});
function registerAlgorithm(algo) {
const name = algo.name;
if (algorithmsByName[name]) {
throw new Error("name " + name + " already taken");
}
algorithmsByName[name] = algo;
algo.ids.forEach(function (id) {
if (algorithmsById[id]) {
throw new Error("id " + id + " already taken");
}
algorithmsById[id] = algo;
});
globalOptions[name] = Object.assign(Object.create(null), algo.defaults);
if (!DEFAULT_ALGO) {
DEFAULT_ALGO = name;
}
}
// -------------------------------------------------------------------
(function (argon2) {
registerAlgorithm({
name: "argon2",
ids: ["argon2d", "argon2i", "argon2id"],
defaults: require("argon2").defaults,
getOptions: function (hash, info) {
let rawOptions = info.options;
let options = {};
// Since Argon2 1.3, the version number is encoded in the hash.
let version;
if (rawOptions.slice(0, 2) === "v=") {
version = +rawOptions.slice(2);
const index = hash.indexOf(rawOptions) + rawOptions.length + 1;
rawOptions = hash.slice(index, hash.indexOf("$", index));
}
rawOptions.split(",").forEach(function (datum) {
const index = datum.indexOf("=");
if (index === -1) {
options[datum] = true;
} else {
options[datum.slice(0, index)] = datum.slice(index + 1);
}
});
options = {
memoryCost: +options.m,
parallelism: +options.p,
timeCost: +options.t,
};
if (version !== undefined) {
options.version = version;
}
return options;
},
hash: argon2.hash,
needsRehash: argon2.needsRehash,
verify: function (password, hash) {
return argon2.verify(hash, password);
},
});
})(require("argon2"));
(function (bcrypt) {
registerAlgorithm({
name: "bcrypt",
ids: ["2", "2a", "2b", "2x", "2y"],
defaults: { cost: 10 },
getOptions: function (_, info) {
return {
cost: +info.options,
};
},
hash: function (password, options) {
return bcrypt.genSalt(options.cost).then(function (salt) {
return bcrypt.hash(password, salt);
});
},
needsRehash: function (_, info) {
const id = info.id;
if (id !== "2a" && id !== "2b" && id !== "2y") {
return true;
}
// Otherwise, let the default algorithm decides.
},
verify: function (password, hash) {
// See: https://github.com/ncb000gt/node.bcrypt.js/issues/175#issuecomment-26837823
if (hash.startsWith("$2y$")) {
hash = "$2a$" + hash.slice(4);
}
return bcrypt.compare(password, hash);
},
});
})(promisifyAll(require("bcryptjs")));
// -------------------------------------------------------------------
const getHashInfo = (function (HASH_RE) {
return function getHashInfo(hash) {
const matches = hash.match(HASH_RE);
if (!matches) {
throw new Error("invalid hash " + hash);
}
return {
id: matches[1],
options: matches[2],
};
};
})(/^\$([^$]+)\$([^$]*)\$/);
function getAlgorithmByName(name) {
const algo = algorithmsByName[name];
if (!algo) {
throw new Error("no available algorithm with name " + name);
}
return algo;
}
function getAlgorithmFromId(id) {
const algo = algorithmsById[id];
if (!algo) {
throw new Error("no available algorithm with id " + id);
}
return algo;
}
function getAlgorithmFromHash(hash) {
return getAlgorithmFromId(getHashInfo(hash).id);
}
// ===================================================================
/**
* Hashes a password.
*
* @param {string} password The password to hash.
* @param {integer} algo Identifier of the algorithm to use.
* @param {object} options Options for the algorithm.
* @param {function} callback Optional callback.
*
* @return {object} A promise which will receive the hashed password.
*/
function hash(password, algo, options) {
algo = getAlgorithmByName(algo || DEFAULT_ALGO);
return algo.hash(
password,
Object.assign(Object.create(null), globalOptions[algo.name], options),
);
}
exports.hash = makeAsyncWrapper(hash);
/**
* Returns information about a hash.
*
* @param {string} hash The hash you want to get information from.
*
* @return {object} Object containing information about the given
* hash: “algorithm”: the algorithm used, “options” the options
* used.
*/
function getInfo(hash) {
const info = getHashInfo(hash);
const algo = getAlgorithmFromId(info.id);
info.algorithm = algo.name;
info.options = algo.getOptions(hash, info);
return info;
}
exports.getInfo = getInfo;
/**
* Checks whether the hash needs to be recomputed.
*
* The hash should be recomputed if it does not use the given
* algorithm and options.
*
* @param {string} hash The hash to analyse.
* @param {integer} algo The algorithm to use.
* @param {options} options The options to use.
*
* @return {boolean} Whether the hash needs to be recomputed.
*/
function needsRehash(hash, algo, options) {
const info = getInfo(hash);
if (info.algorithm !== (algo || DEFAULT_ALGO)) {
return true;
}
const algoNeedsRehash = getAlgorithmFromId(info.id).needsRehash;
const result = algoNeedsRehash && algoNeedsRehash(hash, info);
if (typeof result === "boolean") {
return result;
}
const expected = Object.assign(
Object.create(null),
globalOptions[info.algorithm],
options,
);
const actual = info.options;
for (const prop in actual) {
const value = actual[prop];
if (typeof value === "number" && value < expected[prop]) {
return true;
}
}
return false;
}
exports.needsRehash = needsRehash;
/**
* Checks whether the password and the hash match.
*
* @param {string} password The password.
* @param {string} hash The hash.
* @param {function} callback Optional callback.
*
* @return {object} A promise which will receive a boolean.
*/
function verify(password, hash) {
return getAlgorithmFromHash(hash).verify(password, hash);
}
exports.verify = makeAsyncWrapper(verify);