MD5
MD5 is a built-in function in SQL that returns a checksum value, specifically a 32-hexadecimal character string, for the given input. This function utilizes the MD5 hashing algorithm which is commonly used in data integrity verification and password security. It is irreversible, meaning you can't return to the original string from the MD5 string.
MD5(str)
- str: This parameter refers to the string that is to be converted into a checksum.
Example
In SQL, you can use the MD5()
function to hash a string. Here’s an example with MySQL:
Output
Below is the output that you will receive when you execute the above SQL query:
Explanation
When you run this SQL statement, the MD5()
function hashes the string ‘Hello World’, resulting in ‘3e25960a79dbc69b674cd4ec67a72c62’. An MD5 hash is a 32-character string.
MD5(text) RETURNS text
- text: This is the input parameter which takes the string that needs to be encoded into the MD5 hash. MD5 function generates a 32-character hexadecimal number, returning the result as a text string.
Example
Output
Explanation
In the provided SQL example, the MD5() function is utilized to return the MD5 hash of ‘example’. The output is a 32-character hexadecimal number, ‘1a79a4d60de6718e8e5b326e338ae533’, which is the MD5 hash for the string ‘example’.
HASHBYTES(‘MD5’, @input)
- ‘md5’: This is a hash algorithm parameter of the HASHBYTES function in SQL Server. It specifies the hash algorithm to be used. When ‘MD5’ is used, the function applies the MD5 algorithm, which generates a 128-bit (16-byte) hash value.
- @input: This represents the input parameter or the string to which the hash algorithm will be applied. It can be any data type that represents string or binary data. HASHBYTES function in SQL Server takes this input and converts it into a hashed value based on the specified algorithm. It returns NULL if the input is NULL.
Example
Output
Explanation
The HASHBYTES
function in SQL Server is used to hash the input varbinary or varchar values. Here, HASHBYTES
is used with ‘MD5’ as the hashing algorithm on the string ‘Hello World’. The result is an MD5 hash of the input string.
DBMS_CRYPTO.HASH (src CLOB CHARACTER SET ANY_CS, typ NUMBER) RETURN RAW;
- src clob character set any_cs: This parameter represents the source data that is to be hashed. The data type is Character Large Object (CLOB) which can hold up to 4GB of data. The character set ANY_CS indicates that the function is able to accept data in any character set.
- typ number: This parameter specifies the cryptographic hash function to be used on the source data. The data type is NUMBER. The value of typ is the result of one of the hash function identifiers provided by the Oracle DBMS_CRYPTO package. Depending upon the value passed, a specific hashing algorithm like SHA1, SHA256, SHA384, or SHA512 gets selected.
Example
Output
Explanation
The example demonstrates the generation of an MD5 hash for a desired string ‘your_password_here’. The DBMS_OBFUSCATION_TOOLKIT.MD5
function in Oracle is used to generate the hash, and after that, RAWTOHEX
function is employed to convert the hash into a human-readable hexadecimal format. The hashed value is selected from the dual table. The output shows the hexadecimal version of the MD5 hash for the given string.