Files
NodeBB/bcrypt.js

35 lines
674 B
JavaScript
Raw Normal View History

2014-08-12 21:41:23 -04:00
'use strict';
2017-02-17 20:20:42 -07:00
var bcrypt = require('bcryptjs');
var async = require('async');
2014-08-12 21:41:23 -04:00
process.on('message', function (msg) {
2014-11-18 22:55:44 -05:00
if (msg.type === 'hash') {
hashPassword(msg.password, msg.rounds);
} else if (msg.type === 'compare') {
2015-03-17 13:09:19 -04:00
bcrypt.compare(msg.password, msg.hash, done);
2014-11-18 22:55:44 -05:00
}
});
function hashPassword(password, rounds) {
async.waterfall([
function (next) {
2014-11-18 22:55:44 -05:00
bcrypt.genSalt(parseInt(rounds, 10), next);
},
function (salt, next) {
2014-11-18 22:55:44 -05:00
bcrypt.hash(password, salt, next);
2017-02-17 19:31:21 -07:00
},
2015-03-17 13:09:19 -04:00
], done);
2014-11-18 22:55:44 -05:00
}
2015-03-17 13:09:19 -04:00
function done(err, result) {
if (err) {
2017-02-18 12:30:49 -07:00
process.send({ err: err.message });
2015-03-17 13:09:19 -04:00
return process.disconnect();
}
2017-02-18 12:30:49 -07:00
process.send({ result: result });
2015-03-17 13:09:19 -04:00
process.disconnect();
2017-02-18 02:30:48 -07:00
}