MD5 Hashing problem

Hello,

I’m new at Golang and familiar with java, and i want to understanding whats the code wanna do
At line 16 i understand that the code return md5 hash
But, can someone explain line 20 to me?

https://play.golang.org/p/Dv0XlpTy5p5

Best Regards,
Nathan

afaik it simply returns the first 16 places of dM

see slices

Hello Max,

I’m still confuse, why value between dM and dI still same after slice the first 16 char?

Thanks

They’re the same because an MD5 digest is 128 bits and 128 bits is 16 bytes, so getting the first 16 bytes of a 16-byte slice does nothing.

As for why the result is being sliced to 16 bytes, I have no idea!

1 Like

It is converting the result of md5.Sum which is an array [16]byte into a slice. I’d say more idiomatically you’d just use [:] to so this.

1 Like

md5.Sum returns a [16]byte, but the example calls md5.New to create a hash.Hash and then calls Sum on that. Perhaps it was refactored from originally calling md5.Sum :man_shrugging:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.