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

时间:2017-10-04 00:35:23

标签: 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逐块处理加密消息,然后传递给PCSSHA256。 p>

1 个答案:

答案 0 :(得分:0)

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