为什么密码没有经过哈希处理?

发布于 2025-01-14 15:03:23 字数 1189 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-01-21 15:03:23

对密码进行哈希处理后,您仍然使用明文密码进行日志记录并将其存储到 prisma 数据库中。
变量hash包含哈希密码。

更改代码以使用 hash 而不是 authDto.password

const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${hash}`);

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 of authDto.password.

const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${hash}`);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文