Golang md5 Sum () function

package main import ( "crypto/md5" "fmt" ) func main() { hash := md5.New() b := []byte("test") fmt.Printf("%x\n", hash.Sum(b)) hash.Write(b) fmt.Printf("%x\n", hash.Sum(nil)) } 

Output:

 *md5.digest74657374d41d8cd98f00b204e9800998ecf8427e 098f6bcd4621d373cade4e832627b4f6 

Can someone explain to me why / how to get a different result for the two prints?

+6
source share
4 answers

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() 
+10
source

You have 2 ways to get the md5.Sum byte fragment:

 func main() { hash := md5.New() b := []byte("test") hash.Write(b) fmt.Printf("way one : %x\n", hash.Sum(nil)) fmt.Printf("way two : %x\n", md5.Sum(b)) } 

According to http://golang.org/src/pkg/crypto/md5/md5.go#L88 , your hash.Sum(b) is like calling append(b, actual-hash-of-an-empty-md5-hash)

Sum Definition:

 func (d0 *digest) Sum(in []byte) []byte { // Make a copy of d0 so that caller can keep writing and summing. d := *d0 hash := d.checkSum() return append(in, hash[:]...) } 

When you call Sum(nil) , it returns d.checkSum() directly as a byte, but if you call Sum([]byte) , it adds d.checkSum() to your input.

+2
source

From the docs:

  // Sum appends the current hash to b and returns the resulting slice. // It does not change the underlying hash state. Sum(b []byte) []byte 

so that "* 74657374 * d41d8cd98f00b204e9800998ecf8427e" is actually the hexadecimal representation of the "test" plus the initial state of the hash.

 fmt.Printf("%x", []byte{"test"}) 

will lead to ... "74657374"!

So basically hash.Sum(b) doesn't do what you think it does. The second statement is the correct hash.

+1
source

I would like to tell you:

why / how to get a different result for two prints?

Ans:

 hash := md5.New() 

When you create a new instance of md5 hash as soon as you call hash.Sum(b) , it is actually an md5 hash for b , since hash itself is empty, so you got 74657374d41d8cd98f00b204e9800998ecf8427e as output.

Now in the next hash.Write(b) expression, you write b to the hash instance, then calling hash.Sum(nil) , it will calculate the md5 for b you just wrote and sum it with the previous value, i.e. . 74657374d41d8cd98f00b204e9800998ecf8427e

It is for this reason that you get these outputs.

For help, refer to the Sum API:

 func (d0 *digest) Sum(in []byte) []byte { 85 // Make a copy of d0 so that caller can keep writing and summing. 86 d := *d0 87 hash := d.checkSum() 88 return append(in, hash[:]...) 89 } 
0
source

Source: https://habr.com/ru/post/970877/


All Articles