【问题标题】:What is an alternative for bcrypt to use with node?bcrypt 与节点一起使用的替代方案是什么?
【发布时间】:2013-11-18 07:31:27
【问题描述】:

我已经尝试了几天在我的 Windows 机器上安装 bcrypt,但没有成功。一个依赖项(Windows 7 SDK)不想安装,即使我已经尝试了来自网络的许多建议,它只是拒绝合作。

我需要一个没有任何依赖关系的 bcrypt 替代品。

【问题讨论】:

  • node的built-in crypto functions有什么问题?
  • @josh3736 我没有足够的加密经验来回答这个问题。但我注意到大多数项目和示例都使用 bcrypt,如果有其他解决方案(如下所述)具有零依赖关系,这似乎很奇怪。

标签: node.js cryptography bcrypt password-hash


【解决方案1】:

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

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

【讨论】:

  • 我可能会使用其中之一,但我不确定您所说的样板文件是什么意思。您是指它做出一些假设的事实吗?比如密钥应该是多少位,加密类型等?除此之外,两者在更容易或更难规避方面有什么不同?
  • 对,simplecrypt 非常擅长只进行简单的字符串编码和解码,而无需任何盐、哈希等配置。bcryptjs 有来自其他模块(例如 npmjs.org/package/loopback)的企业支持,所以我肯定会对于更复杂的东西,选择它而不是 simplecrypt。
  • bcryptjs 是否具有与 bcrypt 相同的安全功能?
【解决方案2】:

您应该真正使用内置的加密模块来满足您的加密需求。它基本上是与 OpenSSL 的绑定,这是一个快速、稳定、安全且经过严格审查的加密库。尝试实施您自己的加密(或使用其他人未经验证的实施加密的尝试)是灾难的根源。

如果您要加密数据,您只需调用crypto.createCipher,它会返回一个可读/可写的Stream。将数据写入流中,它将使用加密数据发出数据事件。

例如:

var stream = crypto.createCipher('aes192', 'mysecretpassword');
stream.on('data', function(enc) {
    // enc is a `Buffer` with a chunk of encrypted data
});

stream.write('some secret data');
stream.end();

【讨论】:

  • 如果您要存储密码,不建议这样做。你需要一个盐,否则你数据库中的所有密码都可能被泄露,例如,使用彩虹表。只是一个想法。干杯!
【解决方案3】:

如果有人遇到类似的问题,你可以试试bcyrptjs,它是用零依赖的JavaScript编写的优化bcrypt,并且兼容C++ bcrypt。

【讨论】:

    【解决方案4】:

    截至 2020 年 4 月 24 日crypto 模块中使用 scrypt 内置了一个很好的哈希密码方法

    // 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");
    
    

    【讨论】:

    • 如何解密?
    • 你不能解密它。它是一种单向哈希。要比较密码,只需对用户输入的文本进行哈希处理,然后将其与您存储的值进行比较。
    【解决方案5】:

    这是 @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 encryptPassowrd = (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 atleast 16 bytes)
      const salt = randomBytes(16).toString('hex');
      return encryptPassowrd(password, salt) + salt;
    };
    
    // fetch the user from your db and then use this function
    
    /**
     * Match password against the stored hash
     */
    export const matchPassword = (passowrd: 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 = encryptPassowrd(passowrd, salt);
      return originalPassHash === currentPassHash;
    };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 2021-11-13
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 2015-12-18
      相关资源
      最近更新 更多