[Angular] Using the Argon 2 Hashing Function In Our Sign Up Backend Service

Which hash algorithom to choose for new application:

https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet

We can use this package:

https://github.com/ranisalt/node-argon2

 

Install:

npm install argon2 --save

 

Code:

复制代码
import {Request, Response} from 'express';
import {db} from './database';
import {USERS} from './database-data';

import * as argon from 'argon2';

export function createUser (req: Request, res: Response) {

  const credentials = req.body;

  argon.hash(credentials.password)
    .then(passwordDigest => {

      const user = db.createUser(credentials.email, passwordDigest);

      console.log(USERS);
      res.status(200).json({id: user.id, email: user.email});
    });

};
复制代码

  

It would be good to add some password validations. So that user cannot enter the password as simple as '123456'...

 

Valid password:

npm install --save password-validatory

 

password-validation.ts:

复制代码
import * as passwordValidator from 'password-validator';

// Create a schema
const schema = new passwordValidator();

// Add properties to it
schema
  .is().min(7)                                    // Minimum length 7
  .has().uppercase()                              // Must have uppercase letters
  .has().lowercase()                              // Must have lowercase letters
  .has().digits()                                 // Must have digits
  .has().not().spaces()                           // Should not have spaces
  .is().not().oneOf(['Passw0rd', 'Password123']); // Blacklist these values

export function validatePassword(password: string) {
  return schema.validate(password, {list: true});
}
复制代码

 

Update code:

复制代码
import {Request, Response} from 'express';
import {db} from './database';
import {USERS} from './database-data';

import * as argon from 'argon2';
import {validatePassword} from './password-validation';

export function createUser (req: Request, res: Response) {

  const credentials = req.body;

  const errors = validatePassword(credentials);

  if (errors.length > 0) {
    res.status(400).json({
      errors
    });
  } else {
    argon.hash(credentials.password)
      .then(passwordDigest => {

        const user = db.createUser(credentials.email, passwordDigest);

        console.log(USERS);
        res.status(200).json({id: user.id, email: user.email});
      });
  }
};
复制代码

 

posted @   Zhentiw  阅读(734)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-08-16 [React Fundamentals] Component Lifecycle - Updating
2016-08-16 [React Fundamentals] Component Lifecycle - Mounting Usage
2016-08-16 [React Fundamentals] Component Lifecycle - Mounting Basics
点击右上角即可分享
微信分享提示