将字节数组从MD5哈希转换为字符串时出现奇怪的问题

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

GO Weirdness Converting Btye array to string from MD5 hash

问题

有人能告诉我我错在哪里吗?

我无法通过将哈希函数生成的字节数组转换为字符串来进行强制转换,我必须使用Sprintf。

以下是代码片段:

f, _ := os.Open(filename)
hash := md5.New()
io.Copy(hash, f)
hashStringGood := fmt.Sprintf("%x", hash.Sum(nil))
hashStringJunk := string(hash.Sum(nil)[:])

hashStringGood 的结果将是 d41d8cd98f00b204e9800998ecf8427e
hashStringJunk 的结果将是 ��ُ�� ���B~

英文:

Could someone enlighten me where I'm going wrong.

I can't convert the byte array produced from the hash sum function by casting with string, I have to use Sprintf.

Here is a snippet of the code:

f, _ := os.Open(filename)
hash := md5.New()
io.Copy(hash, f)
hashStringGood := fmt.Sprintf("%x", hash.Sum(nil))
hashStringJunk := string(hash.Sum(nil)[:])

hasStringGood would result in d41d8cd98f00b204e9800998ecf8427e
hashStringJunk would result in ��ُ�� ���B~

答案1

得分: 3

当你将随机二进制数据转换为没有编码方案的字符串时,很不可能将数据映射为一系列可打印字符。

fmt 包中的 %x 动词是用于十六进制编码二进制数据的便捷方法。在 fmt 包文档 的 "String and slice of bytes" 部分中有关于动词定义的说明:

%s	字符串或切片的未解释字节
%q	使用 Go 语法安全转义的双引号字符串
%x	十六进制,小写,每个字节两个字符

另外,你可以使用嵌套在 encoding 包 下的其他包来对数据进行编码:

package main

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"fmt"
)

func main() {
	hash := md5.Sum([]byte("input to be hashed"))
	fmt.Printf("使用 %%s 动词: %s\n", hash)
	fmt.Printf("使用 %%q 动词: %q\n", hash)
	fmt.Printf("使用 %%x 动词: %x\n", hash)

	hexHash := hex.EncodeToString(hash[:])
	fmt.Printf("转换为十六进制编码的字符串: %s\n", hexHash)

	base64Hash := base64.StdEncoding.EncodeToString(hash[:])
	fmt.Printf("转换为 base64 编码的字符串: %s\n", base64Hash)
}

输出结果

使用 %s 动词: �����Q���6���5�
使用 %q 动词: "\x8d\xa8\xf1\xf8\x06\xd3Q\x9d\xa1\xe46\xdb\xfb\x9f5\xd7"
使用 %x 动词: 8da8f1f806d3519da1e436dbfb9f35d7
转换为十六进制编码的字符串: 8da8f1f806d3519da1e436dbfb9f35d7
转换为 base64 编码的字符串: jajx+AbTUZ2h5Dbb+5811w==

Go Playground

英文:

When you convert random binary data to a string without an encoding scheme, it's very unlikely that the data will map to a sequence of printable characters.

The %x verb from the fmt package is a convenience method for hex-encoding binary data. From the "String and slice of bytes" section of the verb definitions in the fmt package documentation:

%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte

Alternatively, you can use packages nested underneath the encoding package to encode the data:

package main

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"fmt"
)

func main() {
	hash := md5.Sum([]byte("input to be hashed"))
	fmt.Printf("Using %%s verb: %s\n", hash)
	fmt.Printf("Using %%q verb: %q\n", hash)
	fmt.Printf("Using %%x verb: %x\n", hash)

	hexHash := hex.EncodeToString(hash[:])
	fmt.Printf("Converted to a hex-encoded string: %s\n", hexHash)

	base64Hash := base64.StdEncoding.EncodeToString(hash[:])
	fmt.Printf("Converted to a base64-encoded string: %s\n", base64Hash)
}

Output

Using %s verb: �����Q���6���5�
Using %q verb: "\x8d\xa8\xf1\xf8\x06\xd3Q\x9d\xa1\xe46\xdb\xfb\x9f5\xd7"
Using %x verb: 8da8f1f806d3519da1e436dbfb9f35d7
Converted to a hex-encoded string: 8da8f1f806d3519da1e436dbfb9f35d7
Converted to a base64-encoded string: jajx+AbTUZ2h5Dbb+5811w==

Go Playground

答案2

得分: 1

MD5哈希是一个128位的值。如果将其转换为字符串,你会得到16个字节的二进制数据,其中许多是不可打印的。你需要使用fmt.Sprintf或其他方法将其转换为字符串。

英文:

MD5 hash is a 128-bit value. If you convert it to a string, you get 16 bytes of binary data, many of which will be unprintable. You have to convert that to a string using fmt.Sprintf or some other method.

huangapple
  • 本文由 发表于 2022年12月2日 14:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74651756.html
匿名

发表评论

匿名网友

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

确定