oms/backend/app/lib/password.js

30 lines
628 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const crypto = require('crypto');
/**
* 生成密码盐值
* @return {String} 16 位十六进制盐值
*/
function generateSalt() {
return crypto.randomBytes(8).toString('hex');
}
/**
* 对密码做加盐哈希:sha256(password:salt)
* 生产环境建议替换为 bcrypt / scrypt 等慢哈希算法
* @param {String} password 明文密码
* @param {String} salt 盐值
* @return {String} 哈希后的密文
*/
function hashPassword(password, salt) {
return crypto.createHash('sha256')
.update(`${password}:${salt}`)
.digest('hex');
}
module.exports = {
generateSalt,
hashPassword,
};