附加切片后计算 sha256 会给出不同的结果,具体取决于我之前是否打印出切片
来源:stackoverflow
时间:2024-03-16 17:45:28 109浏览 收藏
在使用 Go 语言计算 SHA256 哈希时,附加切片到另一个切片可能会导致意外结果。这是因为在将切片附加到已存在的切片时,Go 编译器可能会覆盖附加切片的某些部分。为了避免这种情况,建议使用“干净”的方式,即创建新切片并逐个附加切片,而不是附加到现有切片。
我正在根据多个字符串计算 sha256。我以特定方式将它们转换为字节切片并将它们全部附加在一起,然后使用内置库计算哈希值。然而,根据我是否在计算 sha256 之前打印出切片,我会奇怪地得到不同的结果。在演示中测试它时,我无法重现它。
可以在 https://play.golang.org/p/z8xkx-p9hug 上查看并运行经过测试的代码,在这两种情况下它实际上给出了相同的结果。
func gethash(input1 string, input2hex string, input3hex string, input4 string) (string, error) { input1bytes := []byte(input1) firsthalfinput1bytes := input1bytes[:8] secondhalfinput1bytes := input1bytes[8:16] input4bytes := []byte(input4) input3bytes, err := hex.decodestring(input3hex) if err != nil { fmt.println("err " + err.error()) } input2bytes, err := hex.decodestring(input2hex) if err != nil { fmt.println("err " + err.error()) } fullhashinputbytes := append(firsthalfinput1bytes, input2bytes...) // this is the optional print which changes output locally: fmt.println("fullhashinputbytes", fullhashinputbytes) fullhashinputbytes = append(fullhashinputbytes, secondhalfinput1bytes...) fullhashinputbytes = append(fullhashinputbytes, input3bytes...) fullhashinputbytes = append(fullhashinputbytes, input4bytes...) sha256 := sha256.sum256(fullhashinputbytes) for i := 0; i < 8; i++ { sha256[i+16] ^= sha256[i+24] } hash := hex.encodetostring(sha256[:24]) fmt.println("hash", hash) return hash, nil }
演示上的日志是
hello, playground fullhashinputbytes [84 72 73 83 73 83 78 79 30 0 22 121 57 203 102 148 210 196 34 172 210 8 160 7] hash 0161d9de8dd815ca9f4e1c7bb8684562542cc24b1026321c hash 0161d9de8dd815ca9f4e1c7bb8684562542cc24b1026321c
但是如果我在本地运行完全相同的代码(只需将其复制粘贴到 main.go 中并执行 go run main.go
或 go build .
和 ./test
)我得到 p>
Hello, playground fullHashInputBytes [84 72 73 83 73 83 78 79 30 0 22 121 57 203 102 148 210 196 34 172 210 8 160 7] hash 0161d9de8dd815ca9f4e1c7bb8684562542cc24b1026321c hash d2de4ffb4e8790b8fd1ceeba726436fd97875a5740c27b47
我使用的是 go 版本 1.13.4
,但 1.10.4
也有同样的问题。我在本地计算机上以及部署到我们的服务器时也遇到了同样的问题。
解决方案
这是因为您通过先附加到 firsthalfinput1bytes
来创建 fullhashinputbytes
:
fullhashinputbytes := append(firsthalfinput1bytes, input2bytes...)
这是 input1bytes
的一部分:
firsthalfinput1bytes := input1bytes[:8]
所以第一个追加可能会覆盖索引大于7的input1bytes
的内容,这实际上是secondhalfinput1bytes
的内容:
secondhalfinput1bytes := input1bytes[8:16]
因此,稍后当您还将 secondhalfinput1bytes
附加到 fullhashinputbytes
时,您最终可能会附加不同的内容。
这很可能不是您想要的。
如果你做得“干净”:
var fullhashinputbytes []byte fullhashinputbytes = append(fullhashinputbytes, firsthalfinput1bytes...) fullhashinputbytes = append(fullhashinputbytes, input2bytes...) // optional print doesn't change anything: fmt.println("fullhashinputbytes", fullhashinputbytes) // ...rest of your appends...
如果您在本地或在 Go Playground 上运行,输出将是相同的。
为什么会有异常行为?
你的第一个追加是否覆盖input1bytes
取决于追加是否可以“就地”执行,而不必分配新的支持数组,这取决于firsthalfinput1bytes
的容量,它是从input1bytes
“继承”的: p>
input1bytes := []byte(input1) fmt.Println(cap(input1bytes))
(您可以在这里阅读更多详细信息:Concatenate two slices in Go)。
conversion []byte(input)
仅保证具有 input1
的字节,但规范并未规定结果切片的容量应该有多大。这可能取决于平台/架构。在 go 演示中,上述转换结果为 capacity = 16
,在我本地的 amd64
架构上,我得到 capacity = 32
。
最后一点:用于 []byte(input)
转换结果切片的容量可能取决于您对结果切片的处理方式。如果将其传递给 fmt.println()
,编译器可能会决定使用较低的容量,因为这表明切片可能会逃逸。再说一次,编译器做出的决定不在你的掌控之中。
不要依赖这样的东西,不要编写依赖于转换结果切片容量的代码。以“干净”的方式进行:不要附加到 firsthalfinput1bytes
而是附加到一个新切片。
理论要掌握,实操不能落!以上关于《附加切片后计算 sha256 会给出不同的结果,具体取决于我之前是否打印出切片》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习