mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			35 lines
		
	
	
		
			674 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			674 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
 | |
| 'use strict';
 | |
| 
 | |
| var bcrypt = require('bcryptjs');
 | |
| var async = require('async');
 | |
| 
 | |
| 
 | |
| process.on('message', function (msg) {
 | |
| 	if (msg.type === 'hash') {
 | |
| 		hashPassword(msg.password, msg.rounds);
 | |
| 	} else if (msg.type === 'compare') {
 | |
| 		bcrypt.compare(msg.password, msg.hash, done);
 | |
| 	}
 | |
| });
 | |
| 
 | |
| function hashPassword(password, rounds) {
 | |
| 	async.waterfall([
 | |
| 		function (next) {
 | |
| 			bcrypt.genSalt(parseInt(rounds, 10), next);
 | |
| 		},
 | |
| 		function (salt, next) {
 | |
| 			bcrypt.hash(password, salt, next);
 | |
| 		},
 | |
| 	], done);
 | |
| }
 | |
| 
 | |
| function done(err, result) {
 | |
| 	if (err) {
 | |
| 		process.send({ err: err.message });
 | |
| 		return process.disconnect();
 | |
| 	}
 | |
| 	process.send({ result: result });
 | |
| 	process.disconnect();
 | |
| }
 |