Hash Functions
Overview
Hash functions are used to calculate hash values of strings. Common hash algorithms such as MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are supported. AI DataLake supports the hash calculation capability in the SQL standard and returns hash values in hexadecimal strings.
For more information, see Hash Functions.
Application Scenarios
- Data verification: Uses MD5 or SHA hashes to verify data integrity.
- Sensitive information anonymization: Performs hash anonymization on sensitive fields (such as ID card numbers and mobile numbers).
- Data partitioning: Uses hash values for data sharding or distributed partition key generation.
- Fingerprint comparison: Uses hash values to quickly determine whether data is consistent.
Notes and Constraints
- The input parameters of all hash functions must be of the STRING type. If the input is NULL, NULL is returned.
- MD5 returns a 32-digit hexadecimal string, SHA-1 returns a 40-digit string, SHA-224 returns a 56-digit string, SHA-256 returns a 64-digit string, SHA-384 returns a 96-digit string, and SHA-512 returns a 128-digit string.
- The hashLength parameter of the SHA2 function supports only the values 224, 256, 384, and 512. An error is reported if other values are used.
- Hash functions are one-way functions and are irreversible. They are not suitable for encryption scenarios.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| MD5(string) | STRING | Returns the MD5 hash value of a string, which is a 32-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns '5d41402abc4b2a76b9719d911017c592'.
SELECT MD5('hello');
-- Calculates the hash value for a table field.
SELECT userId, MD5(userEmail) AS emailHash FROM users;
-- Returns NULL if the input is NULL.
SELECT MD5(CAST(NULL AS STRING)); |
| SHA1(string) | STRING | Returns the SHA-1 hash value of a string, which is a 40-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'.
SELECT SHA1('hello');
-- Calculates the hash value for a table field.
SELECT orderId, SHA1(orderDetail) AS detailHash FROM orders; |
| SHA224(string) | STRING | Returns the SHA-224 hash value of a string, which is a 56-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns a 56-digit hexadecimal string.
SELECT SHA224('hello');
-- Calculates the hash value for a table field.
SELECT userId, SHA224(userName) AS nameHash FROM users; |
| SHA256(string) | STRING | Returns the SHA-256 hash value of a string, which is a 64-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'.
SELECT SHA256('hello');
-- Calculates the hash value for a table field.
SELECT userId, SHA256(password) AS passwordHash FROM users;
-- Returns NULL if the input is NULL.
SELECT SHA256(CAST(NULL AS STRING)); |
| SHA384(string) | STRING | Returns the SHA-384 hash value of a string, which is a 96-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns a 96-digit hexadecimal string.
SELECT SHA384('hello');
-- Calculates the hash value for a table field.
SELECT docId, SHA384(docContent) AS contentHash FROM documents; |
| SHA512(string) | STRING | Returns the SHA-512 hash value of a string, which is a 128-digit hexadecimal string. If the string is NULL, NULL is returned. | -- Returns a 128-digit hexadecimal string.
SELECT SHA512('hello');
-- Calculates the hash value for a table field.
SELECT fileId, SHA512(fileContent) AS contentHash FROM files; |
| SHA2(string, hashLength) | STRING | Returns a hash value using the SHA-2 series hash functions (SHA-224, SHA-256, SHA-384, or SHA-512). The first parameter, string, is the string to be hashed, and the second parameter, hashLength, is the bit length of the result (224, 256, 384, or 512). If string or hashLength is NULL, NULL is returned. | -- Uses SHA-256 (equivalent to the SHA256 function). Returns '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'.
SELECT SHA2('hello', 256);
-- Uses SHA-512 (equivalent to the SHA512 function).
SELECT SHA2('hello', 512);
-- Uses SHA-224 (equivalent to the SHA224 function).
SELECT SHA2('hello', 224);
-- Returns NULL when hashLength is NULL.
SELECT SHA2('hello', CAST(NULL AS INT)); |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot