dongmei3869
dongmei3869
2017-10-04 00:35
浏览 158
已采纳

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

I have a problem with Golang's crypto/sha256 package. I am sending the same input to sha256.Sum256 two separate times, and the output is different each time.

My code is the following:

//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:
||%s||
", tohashinner)
    fmt.Printf("tohashouter in || bounds:
||%s||
", tohashouter)
    fmt.Printf("tohashinnerwmess in || bounds:
||%s||
", tohashinnerwmess)
    fmt.Printf("firsthashval after Hex Encode in || bounds:
||%s||
", hex.EncodeToString(firsthashval[:]))

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

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

Below is the fmt.Printf output for the first run:

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||

And here is the output for the second run:

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||

In both of the outputs, everything is the same except for firsthashvalue and the resulting tag. Since tohashinnerwmess (a variable I'm using solely to figure out what's going wrong) is equivalent in both cases and it is the only thing being fed into sha256.Sum256, I'm confused as to why firsthashvalue is different for the two inputs.

I'm pretty sure that sha256.Sum256 is deterministic and is expected to return the same output for a given input - tested this in the Go Playground, and indeed it returned the same thing - so I assume the problem lies either with my code or my understanding of Sum256's inner workings. If anyone could explain what I am doing wrong, that would be greatly appreciated.

Thank you.

EDITS: - On the first run, the message input to the PCSSHA256 function is directly read from a file using ioutil.ReadFile() while on the second run, an encrypted message is first processed block by block via AES and then passed to PCSSHA256.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan36000
    duan36000 2017-10-04 01:36
    已采纳

    Answer: It is an issue with my code. My message is padded by 8 extra 0s in the second case when converted to Hex - this padding is not visible when printed as a string.

    点赞 评论

相关推荐