英文:
different hash between Node.js crypto-js MD5 and Golang crypto/md5.Sum()
问题
crypto-js的MD5函数(https://www.npmjs.com/package/crypto-js)如何处理对象?我正在尝试使用crypto/md5(https://pkg.go.dev/crypto/md5)包在Golang中复制哈希行为,但我无法弄清楚为什么我的两个实现的哈希值不同。这一定是因为crypto-js在内部如何处理对象的原因,因为对于纯字符串,我得到的哈希值是相同的。
Node.js示例:
var CryptoJS = require("crypto-js");
let body = {"key":"value"}
let bodyHash = CryptoJS.MD5(body);
CryptoJS.enc.Base64.stringify(hash)
输出哈希值:SH97IvaDEtLBu8k7GupEWw==
Golang示例:
import (
"crypto/md5"
"encoding/base64"
)
type KeyValue struct {
Key string `json:"key"`
}
func main() {
body := KeyValue{
Key: "value",
}
bodyByteArray, _ := json.Marshal(body)
bodyHash := md5.Sum(bodyByteArray)
bodyHashBase64 := base64.StdEncoding.EncodeToString(bodyHash)
}
输出哈希值:pzU/fN3OgI3gAydHoLe+UA==
感谢任何帮助和解释!
英文:
how are objects treated by crypto-js' MD5-function (https://www.npmjs.com/package/crypto-js)? I am trying to replicate the hashing behaviour in Golang using the crypto/md5 (https://pkg.go.dev/crypto/md5) package, but I can't figure out why the hashes are different for my two implementations. It must be something with how crypto-js is treating objects internally, because for pure strings I am getting the same hashes.
Node.js example:
var CryptoJS = require("crypto-js");
let body = {"key":"value"}
let bodyHash = CryptoJS.MD5(body);
CryptoJS.enc.Base64.stringify(hash)
Outputs the hash: SH97IvaDEtLBu8k7GupEWw==
Golang example:
import (
"crypto/md5"
"encoding/base64"
)
type KeyValue struct {
Key string `json:"key"`
}
func main() {
body := KeyValue{
Key: "value",
}
bodyByteArray, _ := json.Marshal(body)
bodyHash := md5.Sum(bodyByteArray)
bodyHashBase64 := base64.StdEncoding.EncodeToString(bodyHash)
}
Outputs the hash: pzU/fN3OgI3gAydHoLe+UA==
Any help and explanations are appreciated!
答案1
得分: 6
你正在对一个对象使用md5,而不是字符串(你正在对不同的东西进行哈希处理)
var CryptoJS = require("crypto-js");
let body = {"key":"value"}
let bodyHash = CryptoJS.MD5(JSON.stringify(body)); <<<<<<<<
CryptoJS.enc.Base64.stringify(hash)
哈希值:pzU/fN3OgI3gAydHoLe+UA==
对于golang
package main
import (
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
)
type KeyValue struct {
Key string `json:"key"`
}
func main() {
body := KeyValue{
Key: "value",
}
bodyByteArray, _ := json.Marshal(body)
fmt.Println(string(bodyByteArray))
digest := md5.New()
digest.Write(bodyByteArray)
bodyHash := digest.Sum(nil)
bodyHashBase64 := base64.StdEncoding.EncodeToString(bodyHash)
fmt.Println(bodyHashBase64)
}
哈希值:pzU/fN3OgI3gAydHoLe+UA==
英文:
You are using md5 on an object not a string (you are hashing different things)
var CryptoJS = require("crypto-js");
let body = {"key":"value"}
let bodyHash = CryptoJS.MD5(JSON.stringify(body)); <<<<<<<<<
CryptoJS.enc.Base64.stringify(hash)
Hash: pzU/fN3OgI3gAydHoLe+UA==
For golang
package main
import (
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
)
type KeyValue struct {
Key string `json:"key"`
}
func main() {
body := KeyValue{
Key: "value",
}
bodyByteArray, _ := json.Marshal(body)
fmt.Println(string(bodyByteArray))
digest := md5.New()
digest.Write(bodyByteArray)
bodyHash := digest.Sum(nil)
bodyHashBase64 := base64.StdEncoding.EncodeToString(bodyHash)
fmt.Println(bodyHashBase64)
}
Hash: pzU/fN3OgI3gAydHoLe+UA==
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论