我正在从多个字符串中计算 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
(,我会得到
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
"继承"的
input1bytes := []byte(input1)
fmt.Println(cap(input1bytes))
(你可以在这里更详细地阅读它:在 Go 中连接两个切片(。
转换[]byte(input)
只保证有input1
的字节,但规范并没有规定结果切片的容量应该有多大。这可能取决于平台/架构。在Go Playground上,上述转换导致capacity = 16
,在我的本地amd64
架构上,我得到了capacity = 32
。
最后一点:用于[]byte(input)
转换结果切片的容量可能取决于您对结果切片执行的操作。如果将容量传递给fmt.Println()
编译器,编译器可能会决定使用较低的容量,因为这表示切片可能会逃逸。同样,编译器做出的决定不在你手中。
不要依赖这样的东西,不要编写依赖于转换结果切片容量的代码。以"干净"的方式进行:不要附加到firstHalfinput1Bytes
而是附加到新切片。