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

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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧