Golang crypto/sha256 - 相同的输入产生不同的输出

标签 go sha256

我对 Golang 的 crypto/sha256 包有疑问。我将相同的输入发送到 sha256.Sum256 两次,每次输出都不同。

我的代码如下:

//Run the HMAC-SHA256 on the given kmac and message and return the generated MAC tag
func PCSSHA256(kmac []byte, message []byte) [32]byte {
    //NOTE: bitwise XOR ^ only works on integers.
    kmac64 := append(kmac, []byte(strings.Repeat("0", 48))[0:]...) //Pad to obtain a 64 byte unit
    tohashinner := make([]byte, 64)
    for i := 0; i < 64; i++ {
            tohashinner[i] = kmac64[i] ^ 0x36
    }
    tohashouter := make([]byte, 64)
    for i := 0; i < 64; i++ {
            tohashouter[i] = kmac64[i] ^ 0x5c
    }
    tohashinnerwmess := append(tohashinner, message[0:]...)
    firsthashval := sha256.Sum256(tohashinnerwmess)

    fmt.Printf("tohashinner in || bounds:\n||%s||\n", tohashinner)
    fmt.Printf("tohashouter in || bounds:\n||%s||\n", tohashouter)
    fmt.Printf("tohashinnerwmess in || bounds:\n||%s||\n", tohashinnerwmess)
    fmt.Printf("firsthashval after Hex Encode in || bounds:\n||%s||\n", hex.EncodeToString(firsthashval[:]))

    tag := sha256.Sum256(append(tohashouter, firsthashval[0:]...))

    fmt.Printf("Input message in || bounds:\n||%s||\n", message)
    fmt.Printf("Input kmac in || bounds:\n||%s||\n", kmac)
    fmt.Printf("Tag generated by PCSSHA256 after Hex Encode in || bounds:\n||%s||\n", hex.EncodeToString(tag[:]))
    return tag
}

下面是第一次运行的 fmt.Printf 输出:

tohashinner in || bounds:
||gdebc`anolwtursp||
tohashouter in || bounds:
||

llllllllllllllllllllllllllllllllllllllllllllllll||
tohashinnerwmess in || bounds:
||gdebc`anolwturspThis is a sample message. It is to be used for testing.
||
firsthashval after Hex Encode in || bounds:
||7b7bec6f1a9e8860a40730f76f3c5a5f3576a90008630e0dc3fbdc088430ce0f||
Input message in || bounds:
||This is a sample message. It is to be used for testing.
||
Input kmac in || bounds:
||QRSTUVWXYZABCDEF||
Tag generated by PCSSHA256 after Hex Encode in || bounds:
||fd54280dbbca722e41024100c8fa171fa640c814cb4c06380efced71d37504d5||

这是第二次运行的输出:

tohashinner in || bounds:
||gdebc`anolwtursp||
tohashouter in || bounds:
||

llllllllllllllllllllllllllllllllllllllllllllllll||
tohashinnerwmess in || bounds:
||gdebc`anolwturspThis is a sample message. It is to be used for testing.
||
firsthashval after Hex Encode in || bounds:
||632b978ee2b3498754b761e3e091832a6e8f8308ea89f55749a0754f481564d1||
Input message in || bounds:
||This is a sample message. It is to be used for testing.
||
Input kmac in || bounds:
||QRSTUVWXYZABCDEF||
Tag generated by PCSSHA256 after Hex Encode in || bounds:
||e81de52ce8c7d86ef9937011e2978b452d300b85051cf68f7bd99572558e3cee||

在这两个输出中,除了 firsthashvalue 和结果标签之外,一切都是一样的。由于 tohashinnerwmess(我仅使用一个变量来弄清楚出了什么问题)在这两种情况下都是等效的,并且它是唯一被输入 sha256.Sum256 的东西,我很困惑为什么两个输入的 firsthashvalue 不同。

我很确定 sha256.Sum256 是确定性的,预计会为给定的输入返回相同的输出 - 在 Go Playground 中对此进行了测试,实际上它返回了相同的东西 - 所以我认为问题出在我的代码或我对 Sum256 内部工作原理的理解。如果有人能解释我做错了什么,那将不胜感激。

谢谢。

编辑: - 在第一次运行时,使用 ioutil.ReadFile() 直接从文件中读取输入到 PCSSHA256 函数的消息,而在第二次运行时,加密消息首先通过 AES 逐 block 处理,然后传递给 PCSSHA256。

最佳答案

回答:这是我的代码的问题。在第二种情况下,当转换为十六进制时,我的消息被 8 个额外的 0 填充 - 此填充在打印为字符串时不可见。

关于Golang crypto/sha256 - 相同的输入产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555379/

相关文章:

javascript - 如何让nodejs与golang对话

ubuntu - 如何从源代码在我的 ubuntu 12.10 上安装 Go

Windows 上的 MySQL : Using SHA-2

java - 无法创建正确的 SHA256 哈希值

unit-testing - 创建模拟函数

go - 如何重复关机并建立go routine?

mongodb - 通过 ssl 将 golang mgo 连接到 mongo

javascript - 使用 HMAC sha256 和 base64 编码请求正文

javascript - 如何使用 nodejs(加密)生成 32 字节的 SHA256 哈希,以避免 tweetnacl.js 抛出错误的 key 大小错误?

iphone - 在 iphone/Objective C 中生成 SHA256 ...?