Search code examples
phpmd5crypt

PHP crypt() - Returned md5 hash


The docs (http://php.net/manual/de/function.crypt.php) for the crypt() function show the following example for an MD5 hash:

$1$rasmusle$rISCgZzpwk3UhDidwXvin0

I understand, that "$1$" is the prefix, which contains the information, that the hash is an MD5 hash.

But how is the rest of the string an MD5 hash? Normally it should be a 32 char string (0-9, a-f), right?

I'm sure, it's a stupid question, but I still want to ask.


Solution

  • Normally it should be a 32 char string (0-9, a-f), right?

    That is not correct (at least strictly speaking). Technically, a MD5 hash is a 128 bit numeric value. The form that you are used to is simply a hexadecimal representation of that number. It is often chosen because they are easy to exchange as strings (128-bit integers are difficult to handle. After all, a typical integer variable usually only holds 64 bit). Consider the following examples:

    1. md5("test") in hexadecimal (base 16) representation: 098f6bcd4621d373cade4e832627b4f6
    2. md5("test") in base 64 representation: CY9rzUYh03PK3k6DJie09g==
    3. md5("test") in decimal (base 10) representation: 12707736894140473154801792860916528374
    4. md5("test") in base 27 representation (never used, just because I can and to prove my point): ko21h9o9h8bc1hgmao4e69bn6f

    All these strings represent the same numerical value, just in different bases.