forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.js
81 lines (64 loc) · 1.91 KB
/
password.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
'use strict';
const path = require('path');
const crypto = require('crypto');
const util = require('util');
const bcrypt = require('bcryptjs');
const fork = require('./meta/debugFork');
function forkChild(message, callback) {
const child = fork(path.join(__dirname, 'password'));
child.on('message', (msg) => {
callback(msg.err ? new Error(msg.err) : null, msg.result);
});
child.on('error', (err) => {
console.error(err.stack);
callback(err);
});
child.send(message);
}
const forkChildAsync = util.promisify(forkChild);
exports.hash = async function (rounds, password) {
password = crypto.createHash('sha512').update(password).digest('hex');
return await forkChildAsync({ type: 'hash', rounds: rounds, password: password });
};
exports.compare = async function (password, hash, shaWrapped) {
const fakeHash = await getFakeHash();
if (shaWrapped) {
password = crypto.createHash('sha512').update(password).digest('hex');
}
return await forkChildAsync({ type: 'compare', password: password, hash: hash || fakeHash });
};
let fakeHashCache;
async function getFakeHash() {
if (fakeHashCache) {
return fakeHashCache;
}
fakeHashCache = await exports.hash(12, Math.random().toString());
return fakeHashCache;
}
// child process
process.on('message', (msg) => {
if (msg.type === 'hash') {
tryMethod(hashPassword, msg);
} else if (msg.type === 'compare') {
tryMethod(compare, msg);
}
});
async function tryMethod(method, msg) {
try {
const result = await method(msg);
process.send({ result: result });
} catch (err) {
process.send({ err: err.message });
} finally {
process.disconnect();
}
}
async function hashPassword(msg) {
const salt = await bcrypt.genSalt(parseInt(msg.rounds, 10));
const hash = await bcrypt.hash(msg.password, salt);
return hash;
}
async function compare(msg) {
return await bcrypt.compare(String(msg.password || ''), String(msg.hash || ''));
}
require('./promisify')(exports);