Louie Miranda
Louie Miranda

Reputation: 1159

Covert Node.js Crypto HMAC to Go-lang HMAC Encryption

I have the following node.js code when doing a password hash.

body.password = covid@19

salt = "hello@world"
body.passwordhex = crypto.createHmac('sha256', salt).update(body.password).digest('hex');

It's giving the following result:

5fbbff7f6b4db4df6308c6ad7e8fd5afcea513bb70ca12073c7bec618c6b4959


Now, I am trying to convert this to it's go-lang equivalent and my code is

body_password := "covid@19"

salt := "hello@world"

// Create a new HMAC by defining the hash type and the key (as byte array)
h := hmac.New(sha256.New, []byte(key))

// Write Data to it
h.Write([]byte(salt))

// Get result and encode as hexadecimal string
hash := hex.EncodeToString(h.Sum(nil))

And the go-lang result is

9b0cb661fcea1bbfe1fa38912e8610f8c0e4707739021988006368c1ba8da8b7

What could be wrong on my go-lang code? Was it the digest?

Upvotes: 1

Views: 559

Answers (1)

thwd
thwd

Reputation: 36

The Javascript code uses salt as the HMAC key and hashes the body_password. Do the same thing in Go to get the same result:

body_password := "covid@19"
salt := "hello@world"
h := hmac.New(sha256.New, []byte(salt))
h.Write([]byte(body_password))
hash := hex.EncodeToString(h.Sum(nil))

Run the program on the GoLang PlayGround: https://play.golang.org/p/GASMDhEhqGi

Upvotes: 2

Related Questions