Archives

The List

« BENCHMARK() :: MySQL Function of the Day | Main | CRC32 :: MySQL Function of the Day »

MD5() :: MySQL Function of the Day

By Carsten | April 17, 2008

Function name: MD5()
Aliases: -
Function type: Encryption/compression function
Purpose: Return the MD5 Message-Digest of a string

Description: MD5() returns a 128-bit (encoded as 32 hex characters) value with a checksum for a string of arbitrary length:

MD5(’bitbybit’): 1e8ef91030bafc87a6491dd641c859ab
MD5(’bitBybit’): a6236c6a6a7cff46e9e5f5f18acc1472

This algorithm is often used to produce checksums for both large and small pieces of data, since it’s very hard to change the original data and still obtain the same MD5 checksum. As you can see in the example above, even small changes in the original text radically changes the resulting MD5 checksum.

One common use of MD5 is the creation of one-way “encryption” of e.g. passwords. One project I worked on had a number of BLOB fields with image data. By having extra columns with the MD5 hashes of the image data, it was easy to find out whether two images differed from one another by checking the (much shorter, and indexable) MD5 sum.

Be careful not to assume that two texts with the same MD5 values are necessarily the same. In the project mentioned, if we found two images with the same MD5 sum, we had to run a check on the complete BLOB to ensure that the image data was in fact equal.

MySQL manual entry on MD5()


MySQL Function of the Day is a small series of concise information regarding most of the functions and operators available in MySQL. Inspired by PHPs funcaday, it’s meant to provide a daily dose of something in-between “Aha!” and “Ho-hum, I knew that already”, depending on your level of experience with MySQL. You can access the entire series with your browser here or pick up the RSS feed here.

Topics: MySQL Function of the day |

2 Responses to “MD5() :: MySQL Function of the Day”

  1. CRC32 :: MySQL Function of the Day | Carsten’s Random Ramblings Says:
    April 18th, 2008 at 10:03 am

    [...] MD5() :: MySQL Function of the Day | [...]

  2. Rohit Says:
    August 14th, 2009 at 4:24 am

    Is there some compressing/uncompressing function in MySQL which exists in a programming language also(in my case PHP) ?
    The requirement is such that a huge string should be compressed in PHP & then stored in a blob. The database team should be able to uncompress it as & when they use procedures to manipulate this data.

Comments