求教Nodejs怎么对密码进行加盐的hash加密?

求教Nodejs怎么对密码进行加盐的hash加密?

以前java项目最近打算用node.js重写,但是加密这里实在没搞定。java中加密是:1024次加盐sha-1加密, 一个例子:salt:47998d63768aa877,密文:bef36ba826b045a7c5e536a2f7131a6c232eee36,明文:yunstudio2013 下面是java代码: private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) { try { MessageDigest digest = MessageDigest.getInstance(algorithm); if (salt != null) { digest.update(salt); } byte[] result = digest.digest(input); for (int i = 1; i < iterations; i++) { digest.reset(); result = digest.digest(result); } return result; } catch (GeneralSecurityException e) { throw Exceptions.unchecked(e); } } 我在js里面是这么干的,但是结果一直不对,代码如下: //bef36ba826b045a7c5e536a2f7131a6c232eee36 var hash = crypto.createHmac(“sha1”, “47998d63768aa877”).update(“yunstudio2013”).digest(“hex”); for (var i = 1; i < 1024; i++) { hash = crypto.createHmac(“sha1”, “47998d63768aa877”).update(hash).digest(“hex”); console.log(hash); } 求给个js代码~~~


6 回复

当然可以!在 Node.js 中实现加盐的 SHA-1 哈希加密可以使用 crypto 模块。根据你的需求,我们可以创建一个函数来模拟你在 Java 代码中的逻辑。

以下是一个完整的示例代码,展示了如何在 Node.js 中实现加盐的 SHA-1 哈希加密:

const crypto = require('crypto');

function sha1HashWithSalt(password, salt, iterations) {
    let hash = crypto.createHmac('sha1', salt).update(password).digest('hex');
    
    for (let i = 1; i < iterations; i++) {
        hash = crypto.createHmac('sha1', salt).update(hash).digest('hex');
    }
    
    return hash;
}

const password = 'yunstudio2013';
const salt = '47998d63768aa877';
const iterations = 1024;

const hashedPassword = sha1HashWithSalt(password, salt, iterations);
console.log(hashedPassword); // 输出结果类似于 "bef36ba826b045a7c5e536a2f7131a6c232eee36"

解释

  1. 引入 crypto 模块:

    const crypto = require('crypto');
    
  2. 定义 sha1HashWithSalt 函数:

    • 接收三个参数:password(明文密码)、salt(加盐值)和 iterations(迭代次数)。
    • 使用 crypto.createHmac('sha1', salt) 创建 HMAC 对象,并使用 update(password) 更新哈希对象的数据。
    • 使用 digest('hex') 获取哈希后的十六进制字符串。
  3. 循环迭代:

    • 在循环中重复上述过程,每次使用上一次的哈希结果作为输入,直到达到指定的迭代次数。
  4. 调用函数并输出结果:

    • 将明文密码、盐值和迭代次数传递给 sha1HashWithSalt 函数,并打印出最终的哈希结果。

这样,你就可以在 Node.js 中实现与 Java 相同的加盐 SHA-1 哈希加密逻辑了。希望这对你有所帮助!


看是不是你需要的 https://github.com/fundon/pbkdf2

给你看两个方法,你参考一下: https://gist.github.com/huaoguo/348cce6ba08c6dc91c29

为了实现类似于Java中的加盐SHA-1哈希加密方法,可以使用Node.js的crypto模块来完成。以下是一个具体的示例代码,该代码实现了与Java中的方法相似的功能:

const crypto = require('crypto');

function sha1HashWithSalt(password, salt, iterations) {
    let hash = crypto.createHmac('sha1', salt).update(password).digest();

    for (let i = 1; i < iterations; i++) {
        hash = crypto.createHmac('sha1', salt).update(hash).digest();
    }

    return hash.toString('hex'); // 返回十六进制表示的哈希值
}

const salt = '47998d63768aa877';
const password = 'yunstudio2013';
const iterations = 1024;

const hashedPassword = sha1HashWithSalt(password, salt, iterations);
console.log(hashedPassword); // 输出最终的哈希值

在这个示例中,我们定义了一个名为sha1HashWithSalt的函数,它接受三个参数:密码、盐值以及迭代次数。函数首先创建一个基于SHA-1算法的HMAC对象,并使用提供的盐值和密码作为输入更新哈希值。然后,在循环中重复这个过程指定的迭代次数。最后,将生成的哈希值转换为十六进制字符串形式并返回。

这个函数应该能产生与Java代码相同的结果。如果遇到任何问题,请确保检查密码、盐值和迭代次数是否正确。

回到顶部