“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:
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.
The query above should return a hash as shown:
|--------------------------------|
|d1901ec2da207bc977c5965c2b4e9e79|
Example 2
You can also provide a numerical value as the parameter. An example is shown below:
Output string:
|--------------------------------|
|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:
Output hash:
|--------------------------------|
|d1901ec2da207bc977c5965c2b4e9e79|
Query 2:
Output hash:
|--------------------------------|
|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.
Output:
|---------|
| |
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!!