0

Very simple CS question. I was reading MD5 documentation, RFC 1321 where it says

The algorithm takes as input a message of arbitrary length and 
produces as output a 128-bit "fingerprint" or "message digest" of the input.

Its saying MD5 generates 128-bit=16bytes hash for a given input.

Then, when I use md5 script in unix/macos or a MD5 online generator, its generating 32 chars long hash, meaning 32bytes. (1 char = 1 byte is my understanding)

eg.

$ md5 <<<"1"
b026324c6904b2a9cb4b88d6d61c81d1


$ printf "b026324c6904b2a9cb4b88d6d61c81d1" | wc -c
      32

But when I try with java MD5 api, it gives me 16 bytes hash which is true according to the documentation.

scala> import java.security.MessageDigest
import java.security.MessageDigest

scala> MessageDigest.getInstance("MD5").digest("1".getBytes)
res0: Array[Byte] = Array(-60, -54, 66, 56, -96, -71, 35, -126, 13, -52, 80, -102, 111, 117, -124, -101)

scala> val hash = MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8")).length
hash: Int = 16

Question is what I missing with the md5 (BSD unix tool).

prayagupa
  • 30,204
  • 14
  • 155
  • 192

1 Answers1

1

The output from md5 is in hexadecimal, also known as base 16. In this format one byte is shown as 2 characters; a 16-byte hash is shown as a 32-character string.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Oh ok, Thanks. Is there way I can get it in base 32. xxd or other tool?. I could check the online tool though - https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html which gives me exact 128 bits – prayagupa Apr 22 '17 at 08:27
  • Why would you want base 32, each digit would encode 5 bits but the length of the hash 128 is not divisible by 5 so you'd need to add padding – Joni Apr 22 '17 at 08:32
  • am still trying to learn something here. I understood MD5 results HexaDecimal which would be `[0-9A-F]` meaning max being [`7fffffffffffffff`](https://en.wikipedia.org/wiki/2,147,483,647#In_computing). How can I convert the Hexadecimal which is hash (eg. `c4ca4238a0b923820dcc509a6f75849b`) to Decimal integer. Is it simply `..... + 9 * 16^1 + b*16^0`? That seem to apply true - http://www.rapidtables.com/convert/number/hex-to-decimal.htm – prayagupa Jun 28 '17 at 18:45
  • 1
    `dc` is convenient base changes on unix. For example, to convert ABC from base 16 to base 10, start dc and type `16 i ABC p`. But hashes are usually given in lower case, you have to convert to upper case first. – Joni Jun 28 '17 at 21:45
  • **`bc` - An arbitrary precision calculator language** seems to work actually. `echo "ibase=16; C4CA4238A0B923820DCC509A6F75849B" | bc` gives me `261578874264819908609102035485573088411` - [Hexadecimal To Decimal in Shell Script](https://stackoverflow.com/a/13280173/432903) – prayagupa Jun 28 '17 at 21:56
  • Dc, bc, personal preference, whatever floats your boat – Joni Jun 29 '17 at 06:20
  • Thanks Joni, couldn't make the syntax of dc working so ended up using bc – prayagupa Jun 29 '17 at 06:22