bcrypt与节点一起使用的替代方案是什么?

Kor*_*ory 18 cryptography bcrypt password-hash node.js

我已经尝试了几天在我的Windows机器上安装bcrypt而没有运气.其中一个依赖项(Windows 7 SDK)不希望安装,即使我已尝试过网络上的许多建议,它只是拒绝合作.

我需要一个很好的替代bcrypt,它没有任何依赖.

end*_*cat 20

查看https://npmjs.org/package/bcryptjs,它与bcrypt完全兼容,没有依赖项.

或者https://npmjs.org/package/simplecrypt如果你不想要加密样板,只需要加密和解密字符串.

  • 我可能会使用其中之一,但我不确定您所说的样板是什么意思。你是指它做出一些假设的事实吗?像密钥应该是多少位,加密类型等?除此之外,就更容易或更难规避而言,两者在其他方面有何不同? (2认同)

mal*_*ala 13

截至2020 年 4 月 24 日scryptcrypto模块中使用了一种很好的内置散列密码方式

// Using the built in crypto module

const { scryptSync, randomBytes } = require("crypto");


// Any random string here (ideally should be atleast 16 bytes)

const salt = randomBytes(16).toString("hex")


// Pass the password string and get hashed password back
// ( and store only the hashed string in your database)

const getHash = (password) => scryptSync(password, salt, 32).toString("hex");

Run Code Online (Sandbox Code Playgroud)


Shi*_*vam 12

这是 @malik-bagwala 的改进版本,带有 JsDocs、类型和匹配密码功能。

import { randomBytes, scryptSync } from 'crypto';

// Pass the password string and get hashed password back
// ( and store only the hashed string in your database)
const encryptPassword = (password: string, salt: string) => {
  return scryptSync(password, salt, 32).toString('hex');
};

/**
 * Hash password with random salt
 * @return {string} password hash followed by salt
 *  XXXX till 64 XXXX till 32
 *
 */
export const hashPassword = (password: string): string => {
  // Any random string here (ideally should be at least 16 bytes)
  const salt = randomBytes(16).toString('hex');
  return encryptPassword(password, salt) + salt;
};

// fetch the user from your db and then use this function

/**
 * Match password against the stored hash
 */
export const matchPassword = (password: string, hash: string): Boolean => {
  // extract salt from the hashed string
  // our hex password length is 32*2 = 64
  const salt = hash.slice(64);
  const originalPassHash = hash.slice(0, 64);
  const currentPassHash = encryptPassword(password, salt);
  return originalPassHash === currentPassHash;
};

Run Code Online (Sandbox Code Playgroud)

  • 使用 [`crypto.timingSafeEqual`](https://nodejs.org/api/crypto.html#cryptotimingsafeequala-b) 而不是 `originalPassHash === currentPassHash` 来比较两个哈希值以防止定时攻击。您可以[在此答案中]找到一个实现(/sf/answers/4692663671/) (2认同)