1.
import * as crypto from 'crypto';
/**
* Make salt
*/
export function makeSalt(): string {
return crypto.randomBytes(3).toString('base64');
}
/**
* Encrypt password
* @param password 解密前的密码
* @param salt 密码盐
*/
export function encryptPassword(password: string, salt: string): string {
if (!password || !salt) {
return '';
}
const tempSalt = Buffer.from(salt, 'base64');
return (
// 10000 代表迭代次数 16代表长度
crypto.pbkdf2Sync(password, tempSalt, 10000, 16, 'sha1').toString('base64')
);
}
2.
import { compare, genSalt, hash } from 'bcryptjs';
export function makeSalt(): string {
return genSalt(10);
}
/**
* Encrypt password
* @param password 解密前的密码
* @param salt 密码盐
*/
export function encryptPassword(password: string, salt: string): string {
if (!password || !salt) {
return '';
}
return hash(password, salt);
}
export function comparePassword(loginPassword: string, userPassword: string): boolean {
return compare(loginPassword, userPassword);
}