Always pass nil to hash.Sum(b []byte)
Do you know the difference between following two functions?
func showMd1(b []byte) { hash := md5.New() md := hash.Sum(b) fmt.Printf("%s\n", hex.EncodeToString(md)) } func showMd2(b []byte) { md := md5.Sum(b) fmt.Printf("%s\n", hex.EncodeToString(md[:])) }
The former appends md5 hash to b. Below is what they prints.
showMd1([]byte{ 1, 2, 3, 4, 5, 6 }) -> 010203040506d41d8cd98f00b204e9800998ecf8427e Hash of empty byte array is appended to 1,2,3,4,5,6. showMd2([]byte{ 1, 2, 3, 4, 5, 6 }) -> 6ac1e56bc78f031059be7be854522c4c Hash of []byte{1,2,3,4,5,6}
It's pretty confusing. hash.Sum() is not the same as final() or digest() in other platforms. In most cases, you should not pass a slice to hash.Sum(). Pass nil to hash.Sum() just like,
hash := md5.New() hash.Write(b) md := hash.Sum(nil)











