首页 > 解决方案 > 隐蔽 Node.js 加密 HMAC 到 Go-lang HMAC 加密

问题描述

在进行密码哈希时,我有以下 node.js 代码。

body.password = covid@19

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

它给出了以下结果:

5fbbff7f6b4db4df6308c6ad7e8fd5afcea513bb70ca12073c7bec618c6b4959


现在,我正在尝试将其转换为它的 go-lang 等效项,我的代码是

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))

go-lang 的结果是

9b0cb661fcea1bbfe1fa38912e8610f8c0e4707739021988006368c1ba8da8b7

我的 go-lang 代码可能有什么问题?是摘要吗?

标签: node.jsgoencryption

解决方案


Javascript 代码salt用作 HMAC 密钥并对body_password. 在 Go 中执行相同的操作以获得相同的结果:

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))

在 GoLang PlayGround 上运行程序:https: //play.golang.org/p/GASMDhEhqGi


推荐阅读