Name

md5()

Synopsis

    string md5 ( string str [, bool raw_output] )

Although the sha1() function is recommended for checksumming data securely, another popular algorithm is MD5, where the "MD" stands for Message Digest. The md5() function produces a data checksum in exactly the same way as sha1(); 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 checksum. However, md5() has a slight speed advantage. Unless you're trying to serve your website from a 386 or have been asked to use a particular algorithm, stick with sha1().

Using md5() is the same as using sha1():

    $md5hash = md5("My string");
    print $md5hash;

Note that if you are thinking that having fewer bits in MD5 makes it less secure, you are correct—but only just. An MD5 checksum is 32 bytes long, which is equal to 128 bits. That is, an MD5 checksum can be made up of 3.402823669209384634-6337460743177e+38 different possibilities, more commonly referred to as 2 to the power of 128. This an enormous number of varieties, and it is quite secure for most purposes.

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.