forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcrypt.js
43 lines (37 loc) · 879 Bytes
/
bcrypt.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
'use strict';
var bcrypt = require('bcryptjs'),
async = require('async');
process.on('message', function(msg) {
if (msg.type === 'hash') {
hashPassword(msg.password, msg.rounds);
} else if (msg.type === 'compare') {
compare(msg.password, msg.hash);
}
});
function hashPassword(password, rounds) {
async.waterfall([
function(next) {
bcrypt.genSalt(parseInt(rounds, 10), next);
},
function(salt, next) {
bcrypt.hash(password, salt, next);
}
], function(err, hash) {
if (err) {
process.send({err: err.message});
return process.disconnect();
}
process.send({result: hash});
process.disconnect();
});
}
function compare(password, hash) {
bcrypt.compare(password, hash, function(err, res) {
if (err) {
process.send({err: err.message});
return process.disconnect();
}
process.send({result: res});
process.disconnect();
});
}