dongou3286 2018-01-09 17:25
浏览 163
已采纳

Golang:sha256为同一输入返回两个不同的值

I'm working on implementing some crypto function which require among other things hash operations (I want to compute a hash of a custom struct) So I wanted to use the SHA256 hash function from the crypto golang package. However, I've noticed that when I run my hash function multiple times for the same input sometimes it returns different values. My understanding is that SHA function return the same hash outputs for a single input value. Below is my implementation of the hash function:

func myHash(s MyStruct) []byte{

    bytes := []byte(fmt.Sprintf("%v", s))

    h := sha256.New()
    h.Write(bytes)

    return h.Sum(nil)
}

And myStruct has the following fields:

type MyStruct struct {
    elliptic.Curve // I use elliptic.P224() as my curve
    X, Y, Z *big.Int
}

The output of myHash function run multiple times for the same input value SInput is as follows:

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[172 219 215 153 250 30 31 136 175 47 25 178 231 84 90 173 31 130 136 131 169 23 98 198 198 109 255 76 228 134 100 191]

[31 114 57 247 85 214 151 174 124 255 5 200 17 200 198 107 105 56 53 126 233 242 228 74 17 87 238 7 136 100 80 160]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

[172 219 215 153 250 30 31 136 175 47 25 178 231 84 90 173 31 130 136 131 169 23 98 198 198 109 255 76 228 134 100 191]

[172 219 215 153 250 30 31 136 175 47 25 178 231 84 90 173 31 130 136 131 169 23 98 198 198 109 255 76 228 134 100 191]

[38 39 88 96 46 192 74 20 93 238 4 18 134 253 238 154 210 89 64 204 13 27 60 78 228 33 57 20 173 80 67 196]

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duanfeng7756 2018-01-09 17:30
    关注

    When you call fmt.Sprintf("%v") and pass pointers (including struct fields which are pointers, such as the P224 Curve contains), it prints the addresses, not the values. Because the addresses will be different every run, you'll get different hashes every run. Try using something deterministic, it will be better-suited to the purpose - maybe JSON or Gob, or write your own simple serializer.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料