MariaDB Tutorial MariaDB Advanced MariaDB Database Account Management MariaDB References

MariaDB SHA2() Function



The MariaDB SHA2() function calculates the SHA-2 family of hash functions (SHA-224, SHA-256, SHA-384, and SHA-512). The first argument is the plaintext string to be hashed. The second argument indicates the desired bit length of the result, which must have a value of 224, 256, 384, 512, or 0 (which is equivalent to 256). If either argument is NULL or the hash length is not one of the permitted values, the return value is NULL. Otherwise, the function result is a hash value containing the desired number of bits.

SHA2() is considered cryptographically more secure than MD5() or SHA1().

Syntax

SHA2(string, hash_length)

Parameters

string Required. Specify the plaintext string to be hashed.
hash_length Required. Specify the desired bit length of the result, which must have a value of 224, 256, 384, 512, or 0 (which is equivalent to 256).

Return Value

Returns a hash value of the string containing the desired number of bits.

Example:

The example below shows the usage of SHA2() function.

SELECT SHA2('xyz', 224);
Result: '30e90f1cd0ceff8eb3dd6a540a605c0666f841d35de63c57e4dd2877'

SELECT SHA2('password', 256);
Result: '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'

SELECT SHA2('alphacodingskills', 224);
Result: 'c1ebaa2fc513c4b540b01652996bd19a78c3322461cf9469e7b57ec6'

SELECT SHA2(123, 224);
Result: '78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f'

SELECT SHA2(NULL, 224);
Result: NULL

❮ MariaDB Functions