-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (57 loc) · 1.82 KB
/
index.js
File metadata and controls
65 lines (57 loc) · 1.82 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
const {fork} = require("child_process");
const bcryptChild = fork(__dirname + "/non-blocking-bcrypt-nodejs");
const q = {};
const genMicroTime = () => {
return Number(process.hrtime().join(""));
};
bcryptChild.on("message", msg => {
const microTime = msg.microTime;
if (msg.err !== undefined) {
q[microTime].reject(msg.err);
delete q[microTime];
return;
}
if (msg.match !== undefined) {
msg = msg.match;
}
q[microTime].resolve(msg);
delete q[microTime];
});
const bcryptChildRequest = (params) => {
params.microTime = genMicroTime();
q[params.microTime] = {};
const p = new Promise((resolve, reject) => {
q[params.microTime].resolve = resolve;
q[params.microTime].reject = reject;
});
q[params.microTime].p = p;
bcryptChild.send(params);
return q[params.microTime].p;
};
exports.genSalt = (rounds = 10) => {
return bcryptChildRequest({method: "genSalt", rounds});
};
exports.genHash = (salt, password) => {
if (!salt || !password) {
return Promise.reject("salt and password are required");
}
return bcryptChildRequest({method: "genHash", salt, password});
};
exports.compare = async (password, hashedPassword) => {
if (!password || !hashedPassword) {
return Promise.reject("password and hashedPassword are required");
}
return bcryptChildRequest({method: "compare", password, hashedPassword});
};
exports.saltAndHash = async (password, rounds) => {
if (!password) {
return Promise.reject("password is required");
}
return bcryptChildRequest({method: "saltAndHash", password, rounds});
};
exports.getRounds = async (encrypted) => {
if (!encrypted) {
return Promise.reject("password is required");
}
return bcryptChildRequest({method: "compare", encrypted});
};