View a markdown version of this page

Hash functions - Amazon Athena

Hash functions

Use these functions to compute MD5, SHA-1, and SHA-256 hashes of strings and blobs, as hexadecimal text or as numbers. MD5 and SHA-1 are not secure for cryptographic use, so use SHA-256 when you need a secure hash.

md5 function

Computes the MD5 hash of value and returns it as a VARCHAR value of 32 lowercase hexadecimal digits. For a VARCHAR value, the function hashes its UTF-8 bytes, so md5('abc') and md5(encode('abc')) return the same hash. If value is NULL, the result is NULL.

To get the hash as a number instead, use the md5_number function.

Syntax

md5(value)

Arguments

The md5 function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

VARCHAR

Examples

The following SQL query returns the MD5 hash of a string.

SELECT md5('abc') AS result

The query returns the following result.

result

900150983cd24fb0d6963f7d28e17f72

The following SQL query returns the MD5 hash of a BLOB of two bytes.

SELECT md5('\xAA\xBB'::BLOB) AS result

The query returns the following result.

result

58cea1f6b2b06520613e09af90dc1c47

The following SQL query shows that a string and its UTF-8 bytes have the same hash.

SELECT md5('abc') = md5(encode('abc')) AS same_hash

The query returns the following result.

same_hash

true

Considerations

md5 accepts only VARCHAR and BLOB values. To hash a number or a value of another type, cast it to VARCHAR first, as in md5(CAST(123 AS VARCHAR)).

MD5 is not secure for cryptographic use. When you need a secure hash, use the sha256 function.

Differences from Athena engine version 3

In Athena engine version 3, md5 takes a VARBINARY value and returns the hash as VARBINARY, so you write to_hex(md5(to_utf8(string))) to get text, which has uppercase hexadecimal digits. In DuckDB, md5 takes the string directly and returns lowercase hexadecimal text. To get the same text as Athena engine version 3, use upper(md5(string)).

md5_number function

Computes the MD5 hash of value and returns the 16-byte digest as a UHUGEINT, an unsigned 128-bit integer. The function reads the digest as a little-endian number, so the first byte of the digest is the least significant byte of the result. If value is NULL, the result is NULL.

To get 64 of the bits as a UBIGINT, use the md5_number_lower function or the md5_number_upper function. To get the hash as hexadecimal text, use the md5 function.

Syntax

md5_number(value)

Arguments

The md5_number function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

UHUGEINT

Examples

The following SQL query returns the MD5 hash of a string as a number.

SELECT md5_number('abc') AS result

The query returns the following result.

result

152195979970564155685860391459828531600

The following SQL query compares the hexadecimal form of the number with the result of md5. The 16 bytes are the same, in reverse order.

SELECT lower(to_hex(md5_number('abc'))) AS number_hex, md5('abc') AS md5_hex

The query returns the following result.

number_hex md5_hex

727fe1287d3f96d6b04fd23c98500190

900150983cd24fb0d6963f7d28e17f72

Considerations

Because the digest is read as a little-endian number, the hexadecimal form of the result has the bytes of the digest in reverse order. As a result, to_hex(md5_number(value)) does not equal upper(md5(value)). The to_hex function also leaves out leading zeros. When the last two digits of md5(value) start with 0, the result therefore has fewer than 32 digits. For example, md5('32') ends in 01, and to_hex(md5_number('32')) has 31 digits.

Iceberg tables cannot store UHUGEINT values. To store the result in an Iceberg table, cast it to another type, such as VARCHAR.

MD5 is not secure for cryptographic use. When you need a secure hash, use the sha256 function.

md5_number_lower function

Computes the MD5 hash of value and returns 64 of its 128 bits as a UBIGINT. The result equals md5_number(value) >> 64, the most significant 64 bits of the number that md5_number returns. In terms of the 16-byte MD5 digest, these are the last 8 bytes, read as a little-endian integer.

To get the other 64 bits, use the md5_number_upper function. If value is NULL, the result is NULL.

Syntax

md5_number_lower(value)

Arguments

The md5_number_lower function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

UBIGINT

Examples

The following SQL query returns the most significant 64 bits of the MD5 hash number of abc.

SELECT md5_number_lower('abc') AS result

The query returns the following result.

result

8250560606382298838

The following SQL query shows that the result equals the most significant 64 bits of md5_number.

SELECT md5_number_lower('abc') = md5_number('abc') >> 64 AS is_high_bits

The query returns the following result.

is_high_bits

true

Considerations

The suffixes _lower and _upper do not match the bit order of md5_number. md5_number_lower returns the most significant 64 bits, and md5_number_upper returns the least significant 64 bits.

MD5 is not secure for cryptographic use. When you need a secure hash, use the sha256 function.

md5_number_upper function

Computes the MD5 hash of value and returns 64 of its 128 bits as a UBIGINT. The result equals md5_number(value) % 18446744073709551616 (2 to the power of 64), the least significant 64 bits of the number that md5_number returns. In terms of the 16-byte MD5 digest, these are the first 8 bytes, read as a little-endian integer.

To get the other 64 bits, use the md5_number_lower function. If value is NULL, the result is NULL.

Syntax

md5_number_upper(value)

Arguments

The md5_number_upper function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

UBIGINT

Examples

The following SQL query returns the least significant 64 bits of the MD5 hash number of abc.

SELECT md5_number_upper('abc') AS result

The query returns the following result.

result

12704604231530709392

The following SQL query shows that the result equals the least significant 64 bits of md5_number.

SELECT md5_number_upper('abc') = md5_number('abc') % 18446744073709551616 AS is_low_bits

The query returns the following result.

is_low_bits

true

Considerations

The suffixes _upper and _lower do not match the bit order of md5_number. md5_number_upper returns the least significant 64 bits, and md5_number_lower returns the most significant 64 bits.

MD5 is not secure for cryptographic use. When you need a secure hash, use the sha256 function.

sha1 function

Computes the SHA-1 hash of value and returns it as a VARCHAR value of 40 lowercase hexadecimal digits. For a VARCHAR value, the function hashes its UTF-8 bytes, so sha1('abc') and sha1(encode('abc')) return the same hash. If value is NULL, the result is NULL.

For a 64-digit hash, use the sha256 function.

Syntax

sha1(value)

Arguments

The sha1 function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

VARCHAR

Examples

The following SQL query returns the first eight digits and the length of the SHA-1 hash of a string.

SELECT left(sha1('abc'), 8) AS hash_prefix, length(sha1('abc')) AS hash_length

The query returns the following result.

hash_prefix hash_length

a9993e36

40

The following SQL query returns the first eight digits of the SHA-1 hash of a BLOB of two bytes.

SELECT left(sha1('\xAA\xBB'::BLOB), 8) AS hash_prefix

The query returns the following result.

hash_prefix

65b1e351

The following SQL query shows that a string and its UTF-8 bytes have the same hash.

SELECT sha1('abc') = sha1(encode('abc')) AS same_hash

The query returns the following result.

same_hash

true

Considerations

sha1 accepts only VARCHAR and BLOB values. To hash a number or a value of another type, cast it to VARCHAR first.

SHA-1 is not secure for cryptographic use. When you need a secure hash, use the sha256 function.

Differences from Athena engine version 3

In Athena engine version 3, sha1 takes a VARBINARY value and returns the hash as VARBINARY, so you write to_hex(sha1(to_utf8(string))) to get text, which has uppercase hexadecimal digits. In DuckDB, sha1 takes the string directly and returns lowercase hexadecimal text. To get the same text as Athena engine version 3, use upper(sha1(string)).

sha256 function

Computes the SHA-256 hash of value and returns it as a VARCHAR value of 64 lowercase hexadecimal digits. For a VARCHAR value, the function hashes its UTF-8 bytes, so sha256('abc') and sha256(encode('abc')) return the same hash. If value is NULL, the result is NULL.

To get the 32 bytes of the hash as a BLOB, pass the result to unhex.

Syntax

sha256(value)

Arguments

The sha256 function takes the following argument:

value

The value to hash, as a VARCHAR or BLOB value.

Return type

VARCHAR

Examples

The following SQL query returns the first 16 digits and the length of the SHA-256 hash of a string.

SELECT left(sha256('abc'), 16) AS hash_prefix, length(sha256('abc')) AS hash_length

The query returns the following result.

hash_prefix hash_length

ba7816bf8f01cfea

64

The following SQL query returns the first 16 digits of the SHA-256 hash of a BLOB of two bytes.

SELECT left(sha256('\xAA\xBB'::BLOB), 16) AS hash_prefix

The query returns the following result.

hash_prefix

d798d1fac6bd4bb1

The following SQL query converts the hash to a BLOB with unhex and returns its length in bytes.

SELECT octet_length(unhex(sha256('abc'))) AS hash_bytes

The query returns the following result.

hash_bytes

32

Considerations

sha256 accepts only VARCHAR and BLOB values. To hash a number or a value of another type, cast it to VARCHAR first.

Differences from Athena engine version 3

In Athena engine version 3, sha256 takes a VARBINARY value and returns the hash as VARBINARY, so you write to_hex(sha256(to_utf8(string))) to get text, which has uppercase hexadecimal digits. In DuckDB, sha256 takes the string directly and returns lowercase hexadecimal text. To get the same text as Athena engine version 3, use upper(sha256(string)).