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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵