我正在转换节点服务。为此,我需要一个兼容的 md5 哈希(不是用于存储密码!!)生成器。然而,在这个例子中,我不断得到不同的结果:
节点crypto在创建md5s时采用编码参数。
> crypto.createHash("md5").update("1Editor’s notebook: Escaping temptation for turf145468066").digest("hex")
'c7c3210bd977b049f42c487b8c6d0463'
在 golang 中:(test_encode.go)
package main
import (
"crypto/md5"
"encoding/hex"
"testing"
)
func TestFoo(t *testing.T) {
const result = "c7c3210bd977b049f42c487b8c6d0463"
stringToEncode := "1Editor’s notebook: Escaping temptation for turf145468066"
hash := md5.Sum([]byte(stringToEncode))
hashStr := hex.EncodeToString(hash[:])
if hashStr != result {
t.Error("Got", hashStr, "expected", result)
}
}
然后go test test_encode.go结果是:
--- FAIL: TestFoo (0.00s)
encode_test.go:17: Got c3804ddcc59fabc09f0ce2418b3a8335 expected c7c3210bd977b049f42c487b8c6d0463
FAIL
FAIL command-line-arguments 0.006s
我已经将其追踪到节点代码中的encoding参数crypto.update。而事实上,字符串作为’引号字符在里面。如果我指定"utf8"它有效。
crypto.createHash("md5").update("1Editor’s notebook: Escaping temptation for turf145468066", "utf8").digest("hex")
但是:我无法更改节点代码,因此 go 代码必须兼容。关于该怎么做的任何想法?
慕无忌1623718
缥缈止盈
桃花长相依
随时随地看视频慕课网APP
相关分类