如何在Go中获得十六进制编码的md5哈希 [英] How to get hex-encoded md5 hash in Go
问题描述
running_hash:= md5。我试图在Go中获取文件的md5哈希值。新(); //输入hash.Hash
running_hash.Write(data); // data is [] byte
sum:= running_hash.Sum(); // [] uint8根据编译器
但是当我试图获取散列的字符串'总和'( http://golang.org/pkg/hash/ ),通过
sumstring:= string(sum); //返回'Ӿ ]앿 N '或类似的
应该是 d3be9e835dec95bfbef34ebe1fbf03da
。当我尝试以字节为单位进行转换时,我得到了相同的废话,只有不同的字符。
我如何获得散列的字符串?
基本上,你已经得到了二进制数据,但它看起来像你期待的十六进制。查看转换例程的十六进制包,特别是 EncodeToString
。我不是一个Go程序员,但我认为如果你只是将 sum
传递给 hex.EncodeToString
,你会得到您预期的答案。
I'm trying to get the md5 hash of a file in Go, like thus:
running_hash := md5.New(); // type hash.Hash
running_hash.Write(data); // data is []byte
sum := running_hash.Sum(); // []uint8 according to the compiler
But when I try to get the string of the hash's 'sum' (http://golang.org/pkg/hash/), via
sumstring := string(sum); // returns 'Ӿ��]앿��N��' or similar
when the hash is supposed to be d3be9e835dec95bfbef34ebe1fbf03da
. I get the same sort of nonsense, only with different characters, when I try to convert on a byte-by-byte basis.
How am I meant to get the hash's string?
Basically, you've got the binary data but it looks like you're expecting hex. Have a look at the hex package for conversion routines, especially EncodeToString
. I'm not a Go programmer, but I think if you just pass sum
into hex.EncodeToString
, you'll get the answer you expected.
这篇关于如何在Go中获得十六进制编码的md5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!