Node.js crypto-js的MD5和Golang crypto/md5.Sum()之间的哈希值不同。

huangapple go评论94阅读模式
英文:

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(&quot;crypto-js&quot;);
let body = {&quot;key&quot;:&quot;value&quot;}
let bodyHash =  CryptoJS.MD5(JSON.stringify(body)); &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
CryptoJS.enc.Base64.stringify(hash)

Hash: pzU/fN3OgI3gAydHoLe+UA==

For golang

package main

import (
	&quot;crypto/md5&quot;
	&quot;encoding/base64&quot;
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

type KeyValue struct {
	Key string `json:&quot;key&quot;`
}

func main() {
	body := KeyValue{
		Key: &quot;value&quot;,
	}
	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==

huangapple
  • 本文由 发表于 2021年7月13日 23:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/68365681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定