Go语言的SHA3-256算法输出无效?

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

Go lang SHA3-256 gives invalid output?

问题

我正在面对go的SHA3-256函数的奇怪结果:
以下是源代码:

import (
	"golang.org/x/crypto/sha3"
	"encoding/hex"
)

func main() {
	pub, _ := hex.DecodeString("c342dbf7cdd3096c4c3910c511a57049e62847dd5030c7e644bc855acc1fd626")
	h := sha3.Sum256(pub[:])
	fmt.Printf("SHA3 %x\n", h)
	// 期望结果: b32562e67d3ea44ba1367ce134caac49fd305b24cde6716ad3857ac682576379
	// 实际结果: 8a3ccc097f854811f4c49346da9c3bd1745d087ed539fa7817960f3e0ed8a44f
}

我在几个在线转换器上验证了结果,它们都给出了与我的结果不同的结果。

英文:

I am facing strange results fo go SHA3-256 function:
here is source code

import (
	"golang.org/x/crypto/sha3"
	"encoding/hex"
)

func main() {
	pub, _ := hex.DecodeString("c342dbf7cdd3096c4c3910c511a57049e62847dd5030c7e644bc855acc1fd626")
	h := sha3.Sum256(pub[:])
	fmt.Printf("SHA3 %x\n", h)
	// expected: b32562e67d3ea44ba1367ce134caac49fd305b24cde6716ad3857ac682576379
	// received: 8a3ccc097f854811f4c49346da9c3bd1745d087ed539fa7817960f3e0ed8a44f
}

I verified result on couple of online converters, they all give same result, different from mine.

答案1

得分: 3

你假设你测试过的在线转换工具执行的是十六进制解码。实际上它们并不是这样做的;它们只是获取字符串的ASCII/UTF-8/其他值,并使用该值进行处理,也就是对文本进行哈希处理。

这可以通过输入你的 c342... 字符串和输入 hello 字符串进行观察。两者都能正常工作,而 hello 显然不包含十六进制字符。另一种方法是将 c 改为大写的 C,这样也会返回不同的结果,而十六进制解码应该返回相同的字节数组和哈希值。

而且,一个小的Java应用程序确实确认了你的值为:

8a3ccc097f854811f4c49346da9c3bd1745d087ed539fa7817960f3e0ed8a44f

请注意,大多数,如果不是全部的在线工具基本上都是业余密码学爱好者的项目。在实现中可能会出现各种编码/解码问题、错误和不确定性。如果你想测试你的实现,请使用官方的NIST测试向量。

英文:

You are assuming that the online converters that you've tested perform hexadecimal decoding. They don't; they just take the ASCII/UTF-8/whatever value of the string and use that, i.e. they hash the text.

This can be easily observed by inputting your c342... string and then inputting hello. Both will work, while hello obviously doesn't contain hexadecimals. Another way is to start with an uppercase C instead of c, which will also return a different result, while the hexadecimal decoding should return an identical byte array and thus hash.

And a small Java application does indeed confirm your value of:

8a3ccc097f854811f4c49346da9c3bd1745d087ed539fa7817960f3e0ed8a44f

Note that most if not all online tools are basically hobby projects by amateur cryptographers. Expect all kind of encoding / decoding issues, errors and uncertainties in the implementation. If you want to test your implementation, use the official NIST test vectors.

huangapple
  • 本文由 发表于 2017年9月16日 23:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/46255506.html
匿名

发表评论

匿名网友

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

确定