I am trying to generate a Hmac/SHA1 signature using Go, but I'm getting different results than when I test with Node.js or Python.
Here's my code in Go:
signature := hmac.New(sha1.New, []byte(signKey))
signature.Write([]byte(buffer))
return hex.EncodeToString(signature.Sum(nil))
Here's my code in Node.js:
return crypto.createHmac('sha1', signKey).update(buffer).digest('hex');
Python:
return hmac.new(signKey, buffer, sha1).hexdigest()
Can you help figure out what I'm doing wrong?
Thanks!
I am getting identical results in Go and Node.js in my testing. That means that your key and/or buffer must different in Go.
Here is my test code for reference:
Go:
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
)
func main() {
signKey := "12345"
buffer := []byte{1, 2, 3}
signature := hmac.New(sha1.New, []byte(signKey))
signature.Write([]byte(buffer))
println(hex.EncodeToString(signature.Sum(nil)))
}
Node.js:
var crypto = require('crypto');
var signKey = "12345";
var buffer = "\x01\x02\x03";
console.log(
crypto.createHmac('sha1', signKey).update("\x01\x02\x03", "binary").digest('hex')
);