Skip to content

Commit f3ec0ee

Browse files
committed
Fix: fix #26
No response is still a response
1 parent 5745bf2 commit f3ec0ee

1 file changed

Lines changed: 3 additions & 26 deletions

File tree

packages/backend/src/utils/scrypt.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,20 @@ interface ScryptResult {
66
}
77

88
export const hash = (password: string) => new Promise<ScryptResult>((resolve, reject) => {
9-
/*
10-
* As the matter of fact, if `salt` is base64-encoded before it's passed to `scrypt`,
11-
* `scrypt` will treat it as a (UTF-8) string and convert it to a buffer whose length is longer than 16.
12-
* It works, although not as intended.
13-
* The correct implementation is:
14-
* ```js
15-
* // hash
16-
* const salt = randomBytes(16);
17-
* scrypt(password, salt, 64, (err, derivedKey) => {
18-
* // ...
19-
* return resolve({
20-
* salt: salt.toString('base64');
21-
* hash: derivedKey.toString('base64'),
22-
* });
23-
* }
24-
* // verify
25-
* scrypt(password, Buffer.from(salt, 'base64'), 64, (err, derivedKey) => {
26-
* // ...
27-
* }
28-
* ```
29-
* For backward compatibility, we have to adapt to the mistake.
30-
*/
31-
const salt = randomBytes(16).toString('base64');
32-
9+
const salt = randomBytes(16);
3310
scrypt(password, salt, 64, (err, derivedKey) => {
3411
if (err) {
3512
return reject(err);
3613
}
3714
return resolve({
38-
salt,
15+
salt: salt.toString('base64'),
3916
hash: derivedKey.toString('base64'),
4017
});
4118
});
4219
});
4320

4421
export const verify = (hash: string, salt: string, password: string) => new Promise<boolean>((resolve, reject) => {
45-
scrypt(password, salt, 64, (err, derivedKey) => {
22+
scrypt(password, Buffer.from(salt, 'base64'), 64, (err, derivedKey) => {
4623
if (err) {
4724
return reject(err);
4825
}

0 commit comments

Comments
 (0)