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.

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

报告相同问题?

悬赏问题

  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?
  • ¥15 关于CPLEX的问题,请专家解答
  • ¥15 cocos的点击事件 怎么穿透到 原生fragment上。
  • ¥20 基于相关估计的TDOA算法中的加权最小二乘拟合法matlab仿真