As far as is known (from the same Wikipedia), the MD5 hash contains 128 bits (16 bytes). But for some reason, when I use the phd function md5 ('test') and get the hash 098f6bcd4621d373cade4e832627b4f6 , then it weighs 32 bytes (which is correspondingly early to 256 bits). Encoding UTF-8, ANSI.

Tell me please, who knows what I'm missing from the view?

  • one
    Maybe it all depends on the encoding? - Vitaly Zaslavsky
  • Yes it is. If you store the hash as a hexadecimal number, then of course it will occupy 128 bits. And so, you keep as a string - Vitaly Zaslavsky
  • then, it would be interesting in what encoding 128 bits were received. I tried UTF-8 (and UTF8 + BOM), ANSI, UNICODE. They seem to be the most popular. - Sever
  • Well, count it yourself. 32 characters. 128 bits 4 bits for each character. 2 ^ 4 = 16 character choices. Accordingly, this 32-digit hexadecimal number. - Vitaly Zaslavsky

1 answer 1

By itself, the MD5 algorithm returns a 128-bit number. But the md5 function in PHP does not return the number itself, but its string representation. In it, each of the 16 bytes of the number is represented by a two-digit hexadecimal number from 00 to ff. Hence the string of 32 characters.

  • All thanks came. I just had to read the documentation (php) carefully. If the optional argument raw_output is TRUE, then a binary string of 16 characters is returned. - Sever
  • Mmm yes. I kind of said the same thing. Can not understand himself? - Vitaly Zaslavsky
  • one
    You did not say the same thing. You have focused on coding, and the problem is in the representation of numbers. In memory, a hash is 128 bits (16 bytes), but its readable representation is represented by each character byte with two characters (bytes). Hence 32 bytes (256 bits), and Unicode has nothing to do with it. In a clean (unreadable) form, the hash would look like this: " kÍF!ÓsÊÞNƒ&'´ö" (without quotes). - fori1ton
  • one
    Here is the JS code that converts a readable hash into its actual memory representation: var str = '098f6bcd4621d373cade4e832627b4f6'; str.match (/. {2} / g) .map (function (s) {return String.fromCharCode (parseInt ('0x' + s));}). join (''); - fori1ton
  • one
    In fact, md5-hash is a number, but it turns out so large that it is represented as a string describing the bytes of which it consists. In int or long hash does not fit, and in the float appears the error of representation. - fori1ton