I am on already good answers. I'm not sure that Sum is actually the function you want. In the hash.Hash documentation:
// Sum appends the current hash to b and returns the resulting slice. // It does not change the underlying hash state. Sum(b []byte) []byte
This function has a double use case, which seems to mix poorly. Use cases:
- Calculation of the hash of one run
- Multiple Run Output Chain
If you just want to calculate the hash of something, use md5.Sum(data) or
digest := md5.New() digest.Write(data) hash := digest.Sum(nil)
This code, in accordance with the excerpt from the above documentation, will add a checksum from data to nil , as a result of which the checksum data will be set.
If you want to link multiple hash blocks, the second option is to use hash.Sum , you can do it like this:
hashed := make([]byte, 0) for hasData { digest.Write(data) hashed = digest.Sum(hashed) }
This will add the hash of each iteration to the hashes already computed. Probably not what you want.
So, now you can understand why your code is not working. If not, take this commented version of your code ( In-Game ):
hash := md5.New() b := []byte("test") fmt.Printf("%x\n", hash.Sum(b)) // gives 74657374<hash> (74657374 = "test") fmt.Printf("%x\n", hash.Sum([]byte("AAA"))) // gives 414141<hash> (41 = 'A') fmt.Printf("%x\n", hash.Sum(nil)) // gives <hash> as append(nil, hash) == hash fmt.Printf("%x\n", hash.Sum(b)) // gives 74657374<hash> (74657374 = "test") fmt.Printf("%x\n", hash.Sum([]byte("AAA"))) // gives 414141<hash> (41 = 'A') hash.Write(b) fmt.Printf("%x\n", hash.Sum(nil)) // gives a completely different hash since internal bytes changed due to Write()