-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.js
More file actions
284 lines (255 loc) · 7.21 KB
/
Copy pathindex.test.js
File metadata and controls
284 lines (255 loc) · 7.21 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
"use strict";
const { describe, it } = require("node:test");
const assert = require("assert");
const hashy = require("./");
// ===================================================================
const data = {
"bcrypt 1": {
value: "password",
hash: "$2y$04$bCdlo4cUGt5.DpaorjzbN.XUX46/YNj4iKsdTvSQ3UE0pleNR2rjS",
info: {
algorithm: "bcrypt",
id: "2y",
options: {
cost: 4,
},
},
needsRehash: true,
},
"bcrypt 2": {
value: "password",
hash: "$2y$05$P2ZY1eZ3oex3LZJ9bGuRnugsVeq6AXy2wlasiKmYamgDEl6w2dRMG",
info: {
algorithm: "bcrypt",
id: "2y",
options: {
cost: 5,
},
},
needsRehash: false,
},
argon2i: {
value: "password",
hash: "$argon2i$m=4096,t=3,p=1$tbagT6b1YH33niCo9lVzuA$htv/k+OqWk1V9zD9k5DOBi2kcfcZ6Xu3tWmwEPV3/nc",
info: {
algorithm: "argon2",
id: "argon2i",
options: {
memoryCost: 4096,
parallelism: 1,
timeCost: 3,
},
},
needsRehash: true,
},
"argon2i with version": {
value: "password",
hash: "$argon2i$v=19$m=4096,t=3,p=1$BHBji9GuMvFc7SrpWucvcQ$7ITF2KM6dkpqGQQKvdMQrfdZ/uhOuiV0A/ZwjCuManM",
info: {
algorithm: "argon2",
id: "argon2i",
options: {
memoryCost: 4096,
parallelism: 1,
timeCost: 3,
version: 19,
},
},
needsRehash: true,
},
"outdated argon2id": {
value: "password",
hash: "$argon2id$v=19$m=4096,t=3,p=1$y+oVGwJBgBWCU7nx3AJHFw$Athwv027c/e5qcddK4MZ30stAkkDfugD8VeB+3R2lJY",
info: {
algorithm: "argon2",
id: "argon2id",
options: {
memoryCost: 4096,
parallelism: 1,
timeCost: 3,
version: 19,
},
},
needsRehash: true,
},
argon2id: {
value: "password",
hash: "$argon2id$v=19$m=65536,t=3,p=4$Iz7J5To/Eum0iDVAwfYSoQ$rZPYTyvAjdgmbdeRknwGS5ezhfeGCcUl07RVo7caa2o",
info: {
algorithm: "argon2",
id: "argon2id",
options: {
memoryCost: 65536,
parallelism: 4,
timeCost: 3,
version: 19,
},
},
needsRehash: false,
},
};
const forOwn = (object, iteratee) => {
Object.keys(object).forEach((key) => {
iteratee(object[key], key, object);
});
};
// ===================================================================
// Sets a small cost for Bcrypt to speed up the tests.
hashy.options.bcrypt.cost = 5;
describe("hash()", function () {
const hash = hashy.hash;
it("can return a promise", function () {
return hash("test");
});
it("can work with callback", function () {
return new Promise(function (resolve, reject) {
hash("test", function (error, hash) {
if (error) {
return reject(error);
}
resolve(hash);
});
});
});
it("does not creates the same hash twice", function () {
return Promise.all([hash("test"), hash("test")]).then(function (hashes) {
assert.notStrictEqual(hashes[0], hashes[1]);
});
});
it("can be verified", function () {
return hash("test").then((hash) => hashy.verify("test", hash));
});
it("rejects for an unknown algorithm", function () {
return hash("test", "does-not-exist").then(
function () {
throw new Error("expected the promise to be rejected");
},
function (error) {
assert.match(error.message, /no available algorithm with name/);
},
);
});
it("propagates errors to the callback", function () {
return new Promise(function (resolve, reject) {
hash("test", "does-not-exist", function (error) {
if (error === undefined) {
return reject(new Error("expected an error"));
}
try {
assert.match(error.message, /no available algorithm with name/);
resolve();
} catch (assertionError) {
reject(assertionError);
}
});
});
});
});
describe("bcrypt password length limit", function () {
const longPassword = "a".repeat(73);
it("hash() rejects passwords over 72 bytes", function () {
return hashy.hash(longPassword, "bcrypt").then(
function () {
throw new Error("expected the promise to be rejected");
},
function (error) {
assert.match(error.message, /72 bytes/);
},
);
});
it("verify() rejects passwords over 72 bytes", function () {
return hashy.verify(longPassword, data["bcrypt 2"].hash).then(
function () {
throw new Error("expected the promise to be rejected");
},
function (error) {
assert.match(error.message, /72 bytes/);
},
);
});
});
describe("getInfo()", function () {
const getInfo = hashy.getInfo;
forOwn(data, function (datum, name) {
describe(name, function () {
it("returns the algorithm and options", function () {
assert.deepStrictEqual(getInfo(datum.hash), datum.info);
});
});
});
it("throws for a malformed hash", function () {
assert.throws(function () {
getInfo("not-a-hash");
}, /invalid hash/);
});
it("throws for an unknown algorithm id", function () {
assert.throws(function () {
getInfo("$does-not-exist$options$rest");
}, /no available algorithm with id/);
});
});
describe("needsRehash()", function () {
const needsRehash = hashy.needsRehash;
forOwn(data, function (datum, name) {
describe(name, function () {
it("returns true if the algorithm or the options differs", function () {
assert.strictEqual(
needsRehash(datum.hash, datum.info.algorithm),
datum.needsRehash,
);
});
});
});
it("returns true when a different algorithm is requested", function () {
assert.strictEqual(needsRehash(data.argon2id.hash, "bcrypt"), true);
});
it("forces a rehash for legacy Bcrypt ids", function () {
// ids other than 2a, 2b and 2y are always considered outdated,
// regardless of cost.
const hash = data["bcrypt 2"].hash.replace("$2y$", "$2$");
assert.strictEqual(needsRehash(hash, "bcrypt"), true);
});
it("respects custom global options for argon2", function () {
return hashy
.hash("test")
.then(function (hash) {
// Well above argon2's own default, whatever it currently is.
hashy.options.argon2.memoryCost = 1 << 20;
assert.strictEqual(needsRehash(hash, "argon2"), true);
})
.finally(function () {
delete hashy.options.argon2.memoryCost;
});
});
});
describe("verify()", function () {
const verify = hashy.verify;
forOwn(data, function (datum, name) {
describe(name, function () {
it("returns whether the password matches the hash", function () {
return verify(datum.value, datum.hash).then(function (success) {
assert.strictEqual(success, true);
});
});
});
});
it("can work with callback", function () {
return new Promise(function (resolve, reject) {
verify(
data.argon2id.value,
data.argon2id.hash,
function (error, success) {
if (error) {
return reject(error);
}
try {
assert.strictEqual(success, true);
resolve();
} catch (assertionError) {
reject(assertionError);
}
},
);
});
});
});