简体   繁体   English

如何将 Java 中的 Sha-512 hash 转换为其等效的 Node.js

[英]How can I convert Sha-512 hash in Java to its Node.js equivalent

I have a simple hashing function in Java that I rewrote in Node.js but they produce different result.我在 Java 中有一个简单的散列 function ,我在 Node.js 中重写了它,但它们产生不同的结果。

Here is the Java:这是 Java:

public static String get_SHA_512_SecurePassword(String str, String customerId) {
    try {
        MessageDigest instance = MessageDigest.getInstance("SHA-512");
        instance.update(customerId.getBytes(StandardCharsets.UTF_8));
        byte[] digest = instance.digest(str.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(Integer.toString((b & 255) + 256, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

and here is the Node.js equivalent I produced.这是我生产的 Node.js 等价物。

let crypto = require('crypto');

function get_SHA_512_SecurePassword(str, customerId) {
    let hash = crypto.createHash('sha512')
    hash.update(customerId, 'utf8')
    let value = hash.digest(str, 'uft8')
    console.log(value.toString('hex'))
    return value.toString('hex');
}

Can anyone explain what I am doing wrong or why they are different if I reproduced right?谁能解释我做错了什么,或者如果我复制正确,为什么它们会有所不同?

You're very nearly there, the issue is that the .digest function does not take an argument in Node.js, so we'll call .update twice, once with customerId, then with str.你快到了,问题是.digest function 在 Node.js 中没有参数,所以我们将调用.update两次,一次使用 customerId,然后使用 str。 We don't actually need to pass the string encoding to the.update function since utf8 is the default encoding.我们实际上不需要将字符串编码传递给.update function,因为 utf8 是默认编码。

const crypto = require('crypto');

function get_SHA_512_SecurePassword(str, customerId) {
    const hash = crypto.createHash('sha512');
    const digest = hash.update(customerId, "utf-8").update(str, "utf-8").digest();
    return digest.toString("hex");
}

console.log(get_SHA_512_SecurePassword("hello", "world"));

This Node.js example outputs:此 Node.js 示例输出:

3e64afa1cb7d643aa36f63b8d092ad76b1f04ff557abbb3d05f5b9037abf68a6606a8885d51bec8f6f39ee7d0badd504241c3704e777a51c21a9723e285fb9b8

Which should be the same output as the Java code.这应该与 Java 代码相同的 output 代码。

Your problem is that you use the crypto wrong in node.js.您的问题是您在 node.js 中使用了错误的密码。 The digest() method in java is a little different than in crypto in node.js java 中的 digest() 方法与 node.js 中的 crypto 方法略有不同

Java MessageDigest Api Doc Java 消息摘要 Api 文档

digest(byte[] input) Performs a final update on the digest using the specified array of bytes, then completes the digest computation. digest(byte[] input) 使用指定的字节数组对摘要执行最终更新,然后完成摘要计算。

So in java you provide another string in digest and consumes it to produce a new hash and then produces the output因此,在 java 中,您在摘要中提供另一个字符串并使用它来生成新的 hash 然后生成 output

In node.js however the documentation for hmac.digest() states Crypto Doc在 node.js 但是hmac.digest()的文档说明Crypto Doc

hmac.digest([encoding])

Calculates the HMAC digest of all of the data passed using hmac.update().计算使用 hmac.update() 传递的所有数据的 HMAC 摘要。 If encoding is provided a string is returned;如果提供了编码,则返回一个字符串; otherwise a Buffer is returned;否则返回一个缓冲区;

So it will not accept another string to be encoded like you pass str in that function.所以它不会接受另一个要编码的字符串,就像你在 function 中传递str一样。 It will accept only an encoding.它只接受一个编码。

So in your code所以在你的代码中

function get_SHA_512_SecurePassword(str, customerId) {
    let hash = crypto.createHash('sha512')
    hash.update(customerId, 'utf8')
    let value = hash.digest(str, 'uft8') <-----this is wrong
    console.log(value.toString('hex'))
    return value.toString('hex');
}

The following would be the right one以下将是正确的

  function get_SHA_512_SecurePassword(str, customerId) {
        let hash = crypto.createHash('sha512')

        hash.update(customerId, 'utf8')
        hash.update(str, 'utf8')
        let value = hash.digest('hex')

        console.log(value)
        return value;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM