5

I see when people write code on calculating MD5, (even in the documentation), they write like this:

IntegerString[Hash[something, "MD5"], 16, 32]

But I think that Hash[something, "MD5"] already returns the 128-bit MD5, and I observed that IntegerString[Hash[something, "MD5"], 16] would always give the same result as the code above. So why is that 32 present?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
vapor
  • 7,911
  • 2
  • 22
  • 55

2 Answers2

9
IntegerString[Hash["363", "MD5"], 16] // StringLength

(* 30 *)

IntegerString[Hash["a", "MD5"], 16] // StringLength

(* 31 *)

Here's a whopper found Googlin':

IntegerString[Hash["jk8ssl", "MD5"], 16,32]
IntegerString[Hash["jk8ssl", "MD5"], 16] // StringLength

(* 0000000018e6137ac2caab16074784a6 *)
(* 24 *)
ciao
  • 25,774
  • 2
  • 58
  • 139
  • So Hash automatically removes the 0 in the front. I see. – vapor May 25 '15 at 08:17
  • 7
    @Felix: not exactly, Hash returns a number (Integer), for which something like leading zeros does not really exist. It is IntegerString which would not pad the (hex) string representation of that number with zeros unless you tell it... – Albert Retey May 25 '15 at 09:01
5

As of version 11.3.0, it is no longer necessary to use IntegerString.

Also, the correct padding will be added automatically by Hash, for example

Hash["jk8ssl", "MD5", "HexString"]

(* "0000000018e6137ac2caab16074784a6" *)
ilian
  • 25,474
  • 4
  • 117
  • 186