Files
NodeBB/bcrypt.js

34 lines
665 B
JavaScript
Raw Normal View History

2014-08-12 21:41:23 -04:00
'use strict';
2014-10-07 19:49:18 -04:00
var bcrypt = require('bcryptjs'),
2014-11-18 22:55:44 -05:00
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);
}
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) {
process.send({err: err.message});
return process.disconnect();
}
process.send({result: result});
process.disconnect();
2014-08-12 21:41:23 -04:00
}