-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
argon2.cjs
188 lines (166 loc) · 4.84 KB
/
argon2.cjs
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
const assert = require("node:assert");
const { randomBytes, timingSafeEqual } = require("node:crypto");
const { promisify } = require("node:util");
const { deserialize, serialize } = require("@phc/format");
const gypBuild = require("node-gyp-build");
const { hash: bindingsHash } = gypBuild(__dirname);
/** @type {(size: number) => Promise<Buffer>} */
const generateSalt = promisify(randomBytes);
const argon2d = 0;
const argon2i = 1;
const argon2id = 2;
module.exports.argon2d = argon2d;
module.exports.argon2i = argon2i;
module.exports.argon2id = argon2id;
/** @enum {argon2i | argon2d | argon2id} */
const types = Object.freeze({ argon2d, argon2i, argon2id });
/** @enum {'argon2d' | 'argon2i' | 'argon2id'} */
const names = Object.freeze({
[types.argon2d]: "argon2d",
[types.argon2i]: "argon2i",
[types.argon2id]: "argon2id",
});
const defaults = Object.freeze({
hashLength: 32,
timeCost: 3,
memoryCost: 1 << 16,
parallelism: 4,
type: argon2id,
version: 0x13,
});
const limits = Object.freeze({
hashLength: { min: 4, max: 2 ** 32 - 1 },
memoryCost: { min: 1 << 10, max: 2 ** 32 - 1 },
timeCost: { min: 2, max: 2 ** 32 - 1 },
parallelism: { min: 1, max: 2 ** 24 - 1 },
});
module.exports.limits = limits;
/**
* @typedef {Object} Options
* @property {number} [hashLength=32]
* @property {number} [timeCost=3]
* @property {number} [memoryCost=65536]
* @property {number} [parallelism=4]
* @property {keyof typeof names} [type=argon2id]
* @property {number} [version=19]
* @property {Buffer} [salt]
* @property {Buffer} [associatedData]
* @property {Buffer} [secret]
*/
/**
* Hashes a password with Argon2, producing a raw hash
*
* @overload
* @param {Buffer | string} password The plaintext password to be hashed
* @param {Options & { raw: true }} options The parameters for Argon2
* @returns {Promise<Buffer>} The raw hash generated from `plain`
*/
/**
* Hashes a password with Argon2, producing an encoded hash
*
* @overload
* @param {Buffer | string} password The plaintext password to be hashed
* @param {Options & { raw?: boolean }} [options] The parameters for Argon2
* @returns {Promise<string>} The encoded hash generated from `plain`
*/
/**
* @param {Buffer | string} password The plaintext password to be hashed
* @param {Options & { raw?: boolean }} [options] The parameters for Argon2
*/
async function hash(password, options) {
let { raw, salt, ...rest } = { ...defaults, ...options };
for (const [key, { min, max }] of Object.entries(limits)) {
const value = rest[key];
assert(
min <= value && value <= max,
`Invalid ${key}, must be between ${min} and ${max}.`,
);
}
salt = salt ?? (await generateSalt(16));
const {
hashLength,
secret = Buffer.alloc(0),
type,
version,
memoryCost: m,
timeCost: t,
parallelism: p,
associatedData: data = Buffer.alloc(0),
} = rest;
const hash = await bindingsHash({
password: Buffer.from(password),
salt,
secret,
data,
hashLength,
m,
t,
p,
version,
type,
});
if (raw) {
return hash;
}
return serialize({
id: names[type],
version,
params: { m, t, p, ...(data.byteLength > 0 ? { data } : {}) },
salt,
hash,
});
}
module.exports.hash = hash;
/**
* @param {string} digest The digest to be checked
* @param {Object} [options] The current parameters for Argon2
* @param {number} [options.timeCost=3]
* @param {number} [options.memoryCost=65536]
* @param {number} [options.parallelism=4]
* @returns {boolean} `true` if the digest parameters do not match the parameters in `options`, otherwise `false`
*/
function needsRehash(digest, options = {}) {
const { memoryCost, timeCost, version } = { ...defaults, ...options };
const {
version: v,
params: { m, t },
} = deserialize(digest);
return +v !== +version || +m !== +memoryCost || +t !== +timeCost;
}
module.exports.needsRehash = needsRehash;
/**
* @param {string} digest The digest to be checked
* @param {Buffer | string} password The plaintext password to be verified
* @param {Object} [options] The current parameters for Argon2
* @param {Buffer} [options.secret]
* @returns {Promise<boolean>} `true` if the digest parameters matches the hash generated from `plain`, otherwise `false`
*/
async function verify(digest, password, options = {}) {
const { id, ...rest } = deserialize(digest);
if (!(id in types)) {
return false;
}
const {
version = 0x10,
params: { m, t, p, data = "" },
salt,
hash,
} = rest;
const { secret = Buffer.alloc(0) } = options;
return timingSafeEqual(
await bindingsHash({
password: Buffer.from(password),
salt,
secret,
data: Buffer.from(data, "base64"),
hashLength: hash.byteLength,
m: +m,
t: +t,
p: +p,
version: +version,
type: types[id],
}),
hash,
);
}
module.exports.verify = verify;