MD5 C#和golang之间不一致 [英] MD5 Inconsistent between C# and golang
问题描述
我试图从C#移植一个算法。我需要的一个步骤是获取字节数组的md5。
I am attempting to port an algorithm from C# to go. One step I need is to take the md5 of an array of bytes. I cannot seem to get a consistent hash between C# and go implementations.
在C#中我可以做: new MD5CryptoServiceProvider()。ComputeHash(new byte [] {5}))
,我得到 [139 182 193 120 56 100 63 150 145 204 106 77 230 197 23 9]
在go: md5.New()。Sum([] byte {5 })
产生: [5 212 29 140 217 143 0 178 4 233 128 9 152 236 248 66 126 ]
我做的事情是wrond,或者实际上是不同的。我需要能够在go端复制C#的行为。
Am I doing something wrond, or are the implementations actually different. I need to be able to replicate the C# behavior on the go side.
我有一些fiddles可用 go 和 c#检查我的整个实现。
I have some fiddles available for go and c# if you want to check my entire implementation.
推荐答案
您误用了Sum函数的输入。 sum的输入参数用于存储输出,而不是哈希的输入。直接使用 md5.Sum
(它的行为正如您想要的)或写入返回的Hash对象,如示例中所示: http://golang.org/pkg/crypto/md5/#example_New
You are misusing the input to the Sum function. The input parameter to sum is used to store the output, not as input to hash. Use md5.Sum
directly (which behaves as you want) or write to the returned Hash object as demonstrated in the example: http://golang.org/pkg/crypto/md5/#example_New
这篇关于MD5 C#和golang之间不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!