Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Hmac SHA1 generates hash different from Hmac SHA1 in Java

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?

like image 847
Artem Nikitin Avatar asked May 15 '15 06:05

Artem Nikitin


People also ask

Is HMAC SHA1 the same as SHA1?

Remarks. HMACSHA1 is a type of keyed hash algorithm that is constructed from the SHA1 hash function and used as an HMAC, or hash-based message authentication code.

What is HMAC SHA1 algorithm?

HMAC stands for Keyed-Hashing for Message Authentication. It's a message authentication code obtained by running a cryptographic hash function (like MD5, SHA1, and SHA256) over the data (to be authenticated) and a shared secret key. HMAC is specified in RFC 2104. HMACs are almost similar to digital signatures.

Is HMAC secure?

HMAC is a great resistance towards cryptanalysis attacks as it uses the Hashing concept twice. HMAC consists of twin benefits of Hashing and MAC and thus is more secure than any other authentication code. RFC 2104 has issued HMAC, and HMAC has been made compulsory to implement in IP security.


1 Answers

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=
like image 145
icza Avatar answered Sep 20 '22 08:09

icza