我正在从多个字符串计算 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
}
Run Code Online (Sandbox Code Playgroud)
操场上的原木是
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
Run Code Online (Sandbox Code Playgroud)
但是如果我在本地运行完全相同的代码(只需将其复制粘贴到 main.go 中并执行go run main.goorgo 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
Run Code Online (Sandbox Code Playgroud)
我正在使用 go 版本,1.13.4但与1.10.4. 在我的本地机器上和部署到我们的服务器时,我也遇到了同样的问题。
这是因为您fullHashInputBytes通过附加到firstHalfinput1Bytesfirst来创建:
fullHashInputBytes := append(firstHalfinput1Bytes, input2Bytes...)
Run Code Online (Sandbox Code Playgroud)
这是 的一部分input1bytes:
firstHalfinput1Bytes := input1bytes[:8]
Run Code Online (Sandbox Code Playgroud)
所以第一个 append 可能会覆盖input1bytes高于 7的at 索引的内容,这实际上是 的内容secondHalfinput1Bytes:
secondHalfinput1Bytes := input1bytes[8:16]
Run Code Online (Sandbox Code Playgroud)
因此,稍后当您还附加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...
Run Code Online (Sandbox Code Playgroud)
如果您在本地或在Go Playground上运行它,那么输出将是相同的。
您的第一次追加input1bytes是否覆盖取决于追加是否可以“就地”执行,而不必分配新的后备数组,这取决于 的容量firstHalfinput1Bytes,该容量是“继承”自input1bytes:
input1bytes := []byte(input1)
fmt.Println(cap(input1bytes))
Run Code Online (Sandbox Code Playgroud)
(您可以在此处阅读更详细的内容:在 Go 中连接两个切片)。
该转换 []byte(input)只保证拥有的字节input1,但该规范并没有规定所产生的切片的容量有多大应该的。它可能取决于平台/架构。在 Go Playground 上,上面的转换结果是capacity = 16,在我的本地amd64架构上,我得到capacity = 32.
最后一点:用于[]byte(input)转换结果切片的容量可能取决于您对结果切片的处理方式。如果您将其传递给fmt.Println(),编译器可能会决定使用较低的容量,因为这表示切片可能会逃逸。同样,编译器做出的决定不在您的掌控之中。
不要依赖这样的东西,不要编写依赖于转换结果切片容量的代码。以“干净”的方式进行:不要附加到firstHalfinput1Bytes新切片。