Jump to content

The md5( ) Function


iwato

Recommended Posts

QUESTION: If setting the raw_output parameter of the md5() function to 1 is suppose to obtain raw binary data, then why might I be receiving the following jibberish when I run the simple script below.The JIBBERISH: 8p'OlIg(

<?php	$str = 'apple';					var_dump(md5($str, 1));?>

Roddy

Link to comment
Share on other sites

That is binary data, expressed as characters (e.g. every 8 bits are grouped and taken as one character). If you think about it, it wouldn't make sense for a string of zeros and ones to be printed, because the string, for example, "00001100" definitely does not equal the binary number 0b00001100.

Link to comment
Share on other sites

Hi, Synook. Thank you for replying.

That is binary data, expressed as characters (e.g. every 8 bits are grouped and taken as one character).
If this were true, then why does the following statement return 0?
printf('%b', md5($str, 1));

Roddy

Link to comment
Share on other sites

Because the md5() function returns a string, and all strings are equivalent to 0 in PHP.
I don't get it.The following still returns 0.
printf('%b', (int) md5($str, 1));

Roddy

Link to comment
Share on other sites

If you cast a string, any string (that doesn't look like a number), into an integer, you get 0.[1] Try it! Binary, in the case of the md5() function's raw_output mode, means binary data, not a binary number. If you want to get it into numerical form, you'll probably need to use ord().

Link to comment
Share on other sites

To clarify, md5() returns a string representation of a hexadecimal number or the number itself if you get the raw mode.To take 'apple' as an example, that's equal to

1f3870be274f6c49b3e31a0c6728957f

If you put that in your calculator, and translate it to binary, you get

00011111001110000111000010111110001001110100111101101100010010011011001111100011000110100000110001100111001010001001010101111111

take every 8 bits (byte) as a character, which would mean

00011111 00111000 01110000 10111110 00100111 01001111 01101100 01001001 10110011 11100011 00011010 00001100 01100111 00101000 10010101 01111111

and treat THAT as character codes. You should get the same stuff you ended up with on screen.

Link to comment
Share on other sites

Because the md5() function returns a string, and all strings are equivalent to 0 in PHP.
That's not necessarily true, if you convert a string to a number it will look for numbers at the start of the string. If you converted this:1f3870be274f6c49b3e31a0c6728957fthat would return 1, because the string starts with a 1 and then there's a letter before another number. This string would convert to 123:123f70be274f6c49 b3e31a0c6728957fIf a string converts to 0, that usually means it does not start with a number. More information about that here:http://www.php.net/manual/en/language.type...ring.conversioniwato seems confused because he's seeing the results from both string to number conversion, and binary to string conversion, and he's assuming they're related when they're not.
Link to comment
Share on other sites

To clarify, md5() returns a string representation of a hexadecimal number or the number itself if you get the raw mode.
So, if I have understood correctly the following string representation of the MD5 hash
1f3870be274f6c49b3e31a0c6728957f

is a continuous series of concatenated hexidecimal numbers that could be expressed alternatively as follows:

1f:38:70:be:27:4f:6c:49:b3:e3:1a:0c:67:28:95:7f

If I have further understood correctly, the above series of hexidecimal numbers when expressed in binary format appear as:

00011111 00111000 01110000 10111110 00100111 01001111 01101100 01001001 10110011 11100011 00011010 00001100 01100111 00101000 10010101 01111111

Accordingly, the MD5 hash returned by md5() function, when $raw_output option is set to true, is the above series of binary-encoded hexidecimal numbers expressed as a continuous string of binary code -- namely,

00011111001110000111000010111110001001110100111101101100010010011011001111100011000110100000110001100111001010001001010101111111

And finally, the var_dump() function, that I mistakenly used to understand what the $raw_output mode was producing, takes the above string of binary code, tries to covert it into intelligible ASCII code, but fails, because some of the resulting octets lie outside the human-intelligible range of ASCII characters.Have I understood correctly?Are there any omissions or additions?Roddy

Link to comment
Share on other sites

is a continuous series of concatenated hexidecimal numbers that could be expressed alternatively as follows:
Basically yeah, but it's still just a string, it loses its hex connection when it goes from a number to a string. At this point it's just a string.It sounds like you understand everything else though, that's the way it works.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...