Go Hmac SHA1 generates hash different from Hmac SHA1 in Java -
i'm starting learn go , i'm trying rewrite existing small application java go.
i need create base64 hash of input string 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 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 go code generates output not expected. example, input string "qwerty"
, key "key"
java output rid1vimxoaouu3vb1svmchwhfhg=
, go output 9cuw7ray671fl65ye3eexgdghd8=
.
where did make mistakes in go code?
the go code provided gives same output java code.
try on go playground.
output:
rid1vimxoaouu3vb1svmchwhfhg=
you made mistake when called getsignature()
function. call linked example code:
fmt.println(getsignature("qwerty", "key"))
your mistake passed empty input getsignature()
function. calling empty ""
input , "key"
key produces non-expected output provided:
fmt.println(getsignature("", "key"))
output:
9cuw7ray671fl65ye3eexgdghd8=
Comments
Post a Comment