mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			30 lines
		
	
	
		
			771 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			771 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
 | |
| '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});
 | |
| 	});
 | |
| } |