生成的 sha512 字符串与 java 中生成的字符串不同

sha512 string generated is different from string generated in java

我正在尝试生成一个类似于 java 代码中生成的散列,以便稍后可以比较它们以检查数据库中的重复项。

这是 java 代码生成它的方式:

public String getHash(String algorithm, String message, String salt) throws NoSuchAlgorithmException {
        // Create MessageDigest instance for given algorithm
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        md.update(salt.getBytes());
        byte[] bytes = md.digest(message.getBytes());

        // Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16)
                    .substring(1));
        }
        return sb.toString();
    }

这是我用 Go 写的:

func HashSha512(original string) (string, error) {

    salt := "abcde687869"

    originalStrBytes := []byte(original)
    sha512Hasher := sha512.New()
    saltedValueBytes := append(originalStrBytes, []byte(salt)...)
    sha512Hasher.Write(saltedValueBytes)
    hashedBytes := sha512Hasher.Sum(nil)

    s := ""
    var x uint64 = 0x100
    y := byte(x)
    for i := 0; i < len(hashedBytes); i++ {
        s += fmt.Sprintf("%x", ((hashedBytes[i] & 0xff) + y))[1:]
    }

    return s, nil
}

但是生成的字符串不一样

去游乐场link:https://play.golang.com/p/uXaw7y2tklN

生成的字符串是

99461a225184c478b8398c7f0dcc1d3afed107660d08a7282a10f5e2ab6

为java中的相同字符串生成的字符串是

020e93364e5186b7d4ac211cd116425234937d390fcc4e1c554fa1e4bafcb934493047ab841e06f00aa28aabee43b737a6bae2f3fc52e431dde724e691aa952d

我做错了什么?

Go 代码哈希消息 + salt。 Java 代码散列 salt + 消息。交换 Go 代码中的顺序以匹配 Java 代码。

在转换为十六进制时使用整数值而不是字节。使用字节时0x100被转换为零:

s += fmt.Sprintf("%x", ((int(hashedBytes[i]) & 0xff) + 0x100))[1:]

更好的是,使用库函数进行转换。使用 encoding/hex:

return hex.EncodeToString(hashedBytes)

使用 fmt:

return fmt.Sprintf("%x", hashedBytes)

字符串编码为字节的方式可能有所不同。 Java 代码使用平台的默认字符集。假设 Go 应用程序通常使用 UTF-8 编码字符串,则转换后的字节是 UTF-8 编码的。

这是该函数的一个更简单的版本:

func HashSha512hex(original string) (string, error) {
    salt := "abcde6786"
    h := sha512.New()
    io.WriteString(h, salt)
    io.WriteString(h, original)
    s := h.Sum(nil)
    return hex.EncodeToString(s), nil
}