加密和散列
原创...小于 1 分钟
加密
散列
安装
npm i bcrypt
npm i -D @types/bcrypt
使用
import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert } from 'typeorm';
import * as bcrypt from 'bcrypt';
@Entity()
export class App {
@PrimaryGeneratedColumn()
id: number;
@Column()
userName: string;
@Column()
passWord: string;
@BeforeInsert()
async hashPassword() {
this.passWord = await bcrypt.hash(this.passWord, 10);
}
@Column({ default: true })
isActive: boolean;
}
要生成盐,请使用 genSalt 函数:
const salt = await bcrypt.genSalt();
要比较/检查密码,请使用 compare 函数:
const isMatch = await bcrypt.compare(password, hash);
Powered by Waline v2.15.7