如何在Go中获得十六进制编码的md5哈希 [英] How to get hex-encoded md5 hash in Go

查看:388
本文介绍了如何在Go中获得十六进制编码的md5哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆