forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcrypt.js
30 lines (24 loc) · 771 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
'use strict';
var bcrypt = require('bcryptjs');
process.on('message', function(m) {
if (m.type === 'hash') {
hash(m.rounds, m.password);
} else if (m.type === 'compare') {
compare(m.password, m.hash);
}
});
function hash(rounds, password) {
bcrypt.genSalt(rounds, function(err, salt) {
if (err) {
return process.send({type:'hash', err: {message: err.message}});
}
bcrypt.hash(password, salt, function(err, hash) {
process.send({type:'hash', err: err ? {message: err.message} : null, hash: hash, password: password});
});
});
}
function compare(password, hash) {
bcrypt.compare(password, hash, function(err, res) {
process.send({type:'compare', err: err ? {message: err.message} : null, hash: hash, password: password, result: res});
});
}