为什么密码没有经过哈希处理?
我正在使用 Argon2 来散列我的密码,这是我的代码:
import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { AuthDto } from './dto';
import * as argon from 'argon2';
async signup(authDto: AuthDto) {
// generate the password
const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${authDto.password}`);
// save the new user in the db
try {
const user = await this.prisma.user.create({
data: {
email: authDto.email,
hash: authDto.password,
firstname: '',
lastname: '',
},
});
//delete user.hash;
// return the saved user
return user;
} catch (error) {
// test if the error is commimg from prisma
if (error instanceof PrismaClientKnownRequestError) {
// test if the field is duplicated
if (error.code === 'P2002') {
throw new ForbiddenException('Credentials taken'); //NestJS exception
}
}
throw error;
}
}
当我打印散列密码时,我发现它没有散列。
PS:我使用NestJS作为nodeJS后端框架,Manjaro Linux作为操作系统,Argon2作为哈希库。
I'm using Argon2 to hash my password, this is my code:
import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { AuthDto } from './dto';
import * as argon from 'argon2';
async signup(authDto: AuthDto) {
// generate the password
const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${authDto.password}`);
// save the new user in the db
try {
const user = await this.prisma.user.create({
data: {
email: authDto.email,
hash: authDto.password,
firstname: '',
lastname: '',
},
});
//delete user.hash;
// return the saved user
return user;
} catch (error) {
// test if the error is commimg from prisma
if (error instanceof PrismaClientKnownRequestError) {
// test if the field is duplicated
if (error.code === 'P2002') {
throw new ForbiddenException('Credentials taken'); //NestJS exception
}
}
throw error;
}
}
When I print my hashed password, I find it not hashed.
PS : I'm using NestJS as nodeJS backend framework, and Manjaro Linux as OS, Argon2 as hash library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对密码进行哈希处理后,您仍然使用明文密码进行日志记录并将其存储到 prisma 数据库中。
变量
hash
包含哈希密码。更改代码以使用
hash
而不是authDto.password
。After hashing the password you are still using the plaintext password for logging and storing it into the prisma db.
The variable
hash
contains the hashed password.Change the code to use the
hash
instead ofauthDto.password
.