note 44351 deleted from function.md5 by tomsommer
| From: | tomsommer@php.net | Date: | Tue, 03 Aug 2004 16:24:37 +0000 |
| Subject: | note 44351 deleted from function.md5 by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73845@lists.php.net to get a copy of this message | ||
Note Submitter: help@okonekpo.com
----
MD5 is simply a way of taking a bunch of characters which is referred to as the input string and
then converting them to a 32 character string called the message digest or the hash of the input
string.
The input string could have any lenght and be composed of letters, numbers or anything. However, its
message digest or hash will only be returned as a 32 character string made up of only hexadecimal
characters.
This means that the hash is only composed of the numbers 0-9 and the letters a-f. It does not
contain any spaces or quotation marks or any other characters apart from the numbers 0-9 and the
letters a-f.
The MD5 hash of each string is different. It is also case sensitive. Even though the input might be
similar, the hash that is generated from each input is completely different.
For the example, you might assume that the md5 hashes generated from the strings apple, apples,
Apple, and Apples will be similar but take a look at the following:
The MD5 hash of apple is: 1f3870be274f6c49b3e31a0c6728957f
The MD5 hash of Apple is: 9f6290f4436e5a2351f12e03b6433c3c
The MD5 hash of apples is: daeccf0ad3c1fc8c8015205c332f5b42
The MD5 hash of Apples is: 92446c031dbe789c917c6da0d7ab44b9
MD5 encryption is ussually used to encrypt valueable information in a database such as passwords. If
this information is stored as plain text anyone that manages to break into the database could see it
directly and then used for malicious purposes.
Since MD5 is a one-way encryption and cannot be reversed, the only way of knowing what the hash
stands for is by brute force (which means trying repeatedly until the md5 hash generated for the
input matches that of the hash) but this will take a very long time.
To employ md5 encryption for use in a database, you could convert the string into its md5 hash
before storing it in the database and then when the user is signing or loggin in the input supplied
from the html form is converted to its md5 hash before comparing it with what is in the database and
if this hash matches what is in the database, then access can be granted. If it does not match, then
he or she could be returned to an error page.
This is the php code that can be used to generate the md5 hashes shown above.
<?php
$string1 = "apple";
$string2 = "Apple";
$string3 = "apples";
$string4 = "Apples";
$md5hash1 = md5($string1); // generates the md5 hash for apple
$md5hash2 = md5($string2); // generates the md5 hash for Apple
$md5hash3 = md5($string3); // generates the md5 hash for apples
$md5hash4 = md5($string4); // generates the md5 hash for Apples
// Prints out the md5 hash for apple
echo "The MD5 hash of ". $string1 ." is ". $md5hash1 ."<br>";
// Prints out the md5 hash for Apple
echo "The MD5 hash of ". $string2 ." is ". $md5hash2 ."<br>";
// Prints out the md5 hash for apples
echo "The MD5 hash of ". $string3 ." is ". $md5hash3 ."<br>";
// Prints out the md5 hash for Apples
echo "The MD5 hash of ". $string4 ." is ". $md5hash4 ;
?>
In summary, md5 is case sensitive, the message digest is always 32 characters long and these
characters only consists of the numbers 0 thru 9 and the letters a thru f.