Go Hmac SHA1 生成的哈希值与 Java 中的 Hmac SHA1 不同。

huangapple go评论83阅读模式
英文:

Go Hmac SHA1 generates hash different from Hmac SHA1 in Java

问题

我正在帮你翻译以下内容:

我刚开始学习Go,并且正在尝试将我现有的小应用程序从Java重写为Go。

我需要使用Hmac SHA1算法创建输入字符串和密钥的Base64哈希。

我的Java代码:

private String getSignedBody(String input, String key) {
    String result = "";
    try {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(input.getBytes("UTF-8"));
        result = Base64.encodeToString(rawHmac, false);
    } catch (Exception e) {
        Logger.error("Failed to generate signature: " + e.getMessage());
    }
    return result;
}

我的Go代码:

func GetSignature(input, key string) string {
    key_for_sign := []byte(key)
    h := hmac.New(sha1.New, key_for_sign)
    h.Write([]byte(input))
    return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

问题在于Go代码生成的输出与预期不符。例如,对于输入字符串"qwerty"和密钥"key",Java输出将是"RiD1vimxoaouU3VB1sVmchwhfhg=",而Go输出将是"9Cuw7rAY671Fl65yE3EexgdghD8="

我在Go代码中犯了什么错误?

英文:

I'm just starting to learn Go and I'm trying to rewrite my existing small application from Java to Go.

I need to create Base64 hash of input string with key using Hmac SHA1 algorithm.

My Java code:

private String getSignedBody(String input, String key) {
    String result = "";
    try {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(input.getBytes("UTF-8"));
        result = Base64.encodeToString(rawHmac, false);
    } catch (Exception e) {
        Logger.error("Failed to generate signature: " + e.getMessage());
    }
    return result;
}

My Go code:

func GetSignature(input, key string) string {
    key_for_sign := []byte(key)
    h := hmac.New(sha1.New, key_for_sign)
    h.Write([]byte(input))
    return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

The problem is that Go code generates output that is not expected. For example, for input string "qwerty" and key "key" Java output will be RiD1vimxoaouU3VB1sVmchwhfhg= and Go output will be 9Cuw7rAY671Fl65yE3EexgdghD8=.

Where did I make mistakes in the Go code?

答案1

得分: 14

你提供的Go代码与Java代码产生完全相同的输出。

Go Playground上尝试一下。

输出:

RiD1vimxoaouU3VB1sVmchwhfhg=

当你调用GetSignature()函数时,你犯了一个错误。像链接中的示例代码一样调用它:

fmt.Println(GetSignature("qwerty", "key"))

你的错误是将空输入传递给了GetSignature()函数。使用空的""输入和"key"密钥调用它会产生你提供的意外输出:

fmt.Println(GetSignature("", "key"))

输出:

9Cuw7rAY671Fl65yE3EexgdghD8=
英文:

The Go code you provided gives exactly the same output as the Java code.

Try it on the Go Playground.

Output:

RiD1vimxoaouU3VB1sVmchwhfhg=

You made the mistake when you called your GetSignature() function. Call it like the linked example code:

fmt.Println(GetSignature("qwerty", "key"))

Your mistake was that you passed an empty input to your GetSignature() function. Calling it with empty "" input and "key" key produces the non-expected output you provided:

fmt.Println(GetSignature("", "key"))

Output:

9Cuw7rAY671Fl65yE3EexgdghD8=

huangapple
  • 本文由 发表于 2015年5月15日 14:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/30253057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定