计算 sha256 在附加切片后会给出不同的结果,具体取决于我之前是否打印出切片
Calculating sha256 gives different results after appending slices depending on if I print out the slice before or not
我正在从多个字符串计算 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]
所以第一个append可能会覆盖索引高于7的input1bytes
的内容,这实际上是secondHalfinput1Bytes
的内容:
secondHalfinput1Bytes := input1bytes[8:16]
所以稍后当您将 secondHalfinput1Bytes
附加到 fullHashInputBytes
时,您可能最终会附加不同的内容。
这很可能不是您想要的。
如果你这样做了"clean":
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
取决于追加是否可以执行"in-place",而不必分配新的后备数组,这取决于firstHalfinput1Bytes
的容量,这是 "inherited" 来自 input1bytes
:
input1bytes := []byte(input1)
fmt.Println(cap(input1bytes))
(您可以在此处阅读更详细的信息:Concatenate two slices in Go)。
conversion []byte(input)
只保证有 input1
的字节,但规范没有规定结果切片的容量应该有多大。它可能取决于平台/架构。在 Go Playground 上,上述转换结果为 capacity = 16
,在我的本地 amd64
架构中,我得到 capacity = 32
.
最后一点:[]byte(input)
转换结果切片的容量可能取决于您对结果切片的处理方式。如果将它传递给 fmt.Println()
,编译器可能会决定使用较低的容量,因为这表明切片可能会逃逸。同样,编译器做出的决定不在您的手中。
不要依赖这样的东西,不要编写依赖于转换结果切片容量的代码。以 "clean" 方式执行:不要附加到 firstHalfinput1Bytes
而是附加到新切片。
我正在从多个字符串计算 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]
所以第一个append可能会覆盖索引高于7的input1bytes
的内容,这实际上是secondHalfinput1Bytes
的内容:
secondHalfinput1Bytes := input1bytes[8:16]
所以稍后当您将 secondHalfinput1Bytes
附加到 fullHashInputBytes
时,您可能最终会附加不同的内容。
这很可能不是您想要的。
如果你这样做了"clean":
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
取决于追加是否可以执行"in-place",而不必分配新的后备数组,这取决于firstHalfinput1Bytes
的容量,这是 "inherited" 来自 input1bytes
:
input1bytes := []byte(input1)
fmt.Println(cap(input1bytes))
(您可以在此处阅读更详细的信息:Concatenate two slices in Go)。
conversion []byte(input)
只保证有 input1
的字节,但规范没有规定结果切片的容量应该有多大。它可能取决于平台/架构。在 Go Playground 上,上述转换结果为 capacity = 16
,在我的本地 amd64
架构中,我得到 capacity = 32
.
最后一点:[]byte(input)
转换结果切片的容量可能取决于您对结果切片的处理方式。如果将它传递给 fmt.Println()
,编译器可能会决定使用较低的容量,因为这表明切片可能会逃逸。同样,编译器做出的决定不在您的手中。
不要依赖这样的东西,不要编写依赖于转换结果切片容量的代码。以 "clean" 方式执行:不要附加到 firstHalfinput1Bytes
而是附加到新切片。