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?