跳至主要內容

加密和散列

刘春龙原创...小于 1 分钟NodejsNestjs教程文档

加密

散列

安装

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