node.js - 隐蔽 Node.js 加密 HMAC 到 Go-lang HMAC 加密

标签 node.js go encryption

在进行密码哈希时,我有以下 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 代码可能有什么问题?是摘要吗?

最佳答案

Javascript 代码使用 salt作为 HMAC key 并散列 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

关于node.js - 隐蔽 Node.js 加密 HMAC 到 Go-lang HMAC 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61562230/

相关文章:

json - 解码 json 以反射(reflect)结构

java - 在 JCE 中编写加密算法

c# - 自定义非对称密码算法

javascript - Windows 7 上的 Istanbul 尔 Jasmine Node 问题

mongodb - 如何在 node.js 服务器中自动重新连接 mongo?

javascript - 如何运行 index.jade 文件?

javascript - io.emit 与 socket.emit

javascript - 如何使用 websockets 更改模板?

android - 无法解密android Lollipop 中的加密文件

go - 如何实现 goroutines 的管道?