4.7.11 Alternative data hashing: md5()This is NOT the latest copy of this book; click here for the latest version.
string md5 ( string source [, bool raw_output])
Although the sha1() function is recommended for hashing data securely, another popular algorithm is MD5, where the "MD" for Message Digest. The md5() function produces a data hash in exactly the same way as sha1(), with the difference is that it is only 32-bytes long. Because sha1() is longer, it is less likely to have a "collision" - a situation where two different strings share the same hash. However, md5() has a slight speed advantage.
Using md5() is the same as using sha1():
<?php
$md5hash = md5("My string");
print $md5hash; ?>
Note if you are thinking having fewer bits in MD5 makes it less secure, you are correct - but only just. An MD5 hash is 32 bytes long, which is equal to 128 bits. That is, an MD5 hash can be made up of 3.4028236692093846346337460743177e+38 different possibilities, more commonly referred to as 2 to the power of 128. This an enormous number of varieties, and so is quite secure for most purposes.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|