MySQL MariaDB

MySQL Calculate MD5 Checksum

“MD5 is one of the most common hashing algorithms used today. Although it is considered less secure than other hashing functions, it is still instrumental due to its low resource requirements, preserved integrity, etc.

It is also effortless to compare, making it very useful for digital signatures and integrity verifications.”

In this article, however, we will focus on using MySQL built-in functions to calculate the MD5 checksum of a value.

 MySQL MD5() Function

As you probably guessed, to calculate the md5 checksum of a given string using MySQL, we will use the md5() function.

This function is native to MySQL version 4.1 and above. It returns the MD5 128-bit checksum for the input string. The function syntax is as shown:

md5(string)

The resulting value is a 32 hexadecimal digit string. The function outputs NULL on NULL input.

Example 1

The example below shows how you can use the md5() function to generate an MD5 hash of an input string.

select md5('linuxhint');

The query above should return a hash as shown:

|md5('linuxhint')                |
|--------------------------------|
|d1901ec2da207bc977c5965c2b4e9e79|

Example 2

You can also provide a numerical value as the parameter. An example is shown below:

select md5(1234);

Output string:

|md5(1234)                       |
|--------------------------------|
|81dc9bdb52d04dc20036dbd8313ed055|

Example 3

Remember that md5 will return a different hash value given different inputs. For example, consider the hashes from the two examples below:

select md5('linuxhint');

Output hash:

|md5('linuxhint')                |
|--------------------------------|
|d1901ec2da207bc977c5965c2b4e9e79|

Query 2:

select md5('linuxhin');

Output hash:

|md5('linuxhin')                 |
|--------------------------------|
|cbdf70f882619e054c0e7e49e7e12aae|

We can see that the md5 function returns two different values given a specific input.

Example 4

As mentioned, the function will return NULL, given a NULL value.

select md5(NULL);

Output:

|md5(NULL)|
|---------|
|         |

NULL Output

Conclusion

In this tutorial, you learned how we could use the md5() function in MySQL to calculate the md5 checksum of an input string.

Thanks for reading & Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list