

# 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
<a name="athena-duckdb-functions-binary-md5"></a>

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](#athena-duckdb-functions-binary-md5-number).

### Syntax
<a name="athena-duckdb-functions-binary-md5-syntax"></a>

```sql
md5({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-md5-arguments"></a>

The `md5` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-md5-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-md5-examples"></a>

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

```sql
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.

```sql
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.

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

The query returns the following result.


| same\_hash | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-binary-md5-considerations"></a>

`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](#athena-duckdb-functions-binary-sha256).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-md5-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-binary-md5-number"></a>

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](#athena-duckdb-functions-binary-md5-number-lower) or the [`md5_number_upper` function](#athena-duckdb-functions-binary-md5-number-upper). To get the hash as hexadecimal text, use the [`md5` function](#athena-duckdb-functions-binary-md5).

### Syntax
<a name="athena-duckdb-functions-binary-md5-number-syntax"></a>

```sql
md5_number({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-md5-number-arguments"></a>

The `md5_number` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-md5-number-return-type"></a>

`UHUGEINT`

### Examples
<a name="athena-duckdb-functions-binary-md5-number-examples"></a>

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

```sql
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.

```sql
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
<a name="athena-duckdb-functions-binary-md5-number-considerations"></a>

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](#athena-duckdb-functions-binary-sha256).

## `md5_number_lower` function
<a name="athena-duckdb-functions-binary-md5-number-lower"></a>

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](#athena-duckdb-functions-binary-md5-number-upper). If `value` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-binary-md5-number-lower-syntax"></a>

```sql
md5_number_lower({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-md5-number-lower-arguments"></a>

The `md5_number_lower` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-md5-number-lower-return-type"></a>

`UBIGINT`

### Examples
<a name="athena-duckdb-functions-binary-md5-number-lower-examples"></a>

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

```sql
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`.

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

The query returns the following result.


| is\_high\_bits | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-binary-md5-number-lower-considerations"></a>

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](#athena-duckdb-functions-binary-sha256).

## `md5_number_upper` function
<a name="athena-duckdb-functions-binary-md5-number-upper"></a>

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](#athena-duckdb-functions-binary-md5-number-lower). If `value` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-binary-md5-number-upper-syntax"></a>

```sql
md5_number_upper({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-md5-number-upper-arguments"></a>

The `md5_number_upper` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-md5-number-upper-return-type"></a>

`UBIGINT`

### Examples
<a name="athena-duckdb-functions-binary-md5-number-upper-examples"></a>

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

```sql
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`.

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

The query returns the following result.


| is\_low\_bits | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-binary-md5-number-upper-considerations"></a>

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](#athena-duckdb-functions-binary-sha256).

## `sha1` function
<a name="athena-duckdb-functions-binary-sha1"></a>

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](#athena-duckdb-functions-binary-sha256).

### Syntax
<a name="athena-duckdb-functions-binary-sha1-syntax"></a>

```sql
sha1({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-sha1-arguments"></a>

The `sha1` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-sha1-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-sha1-examples"></a>

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

```sql
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.

```sql
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.

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

The query returns the following result.


| same\_hash | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-binary-sha1-considerations"></a>

`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](#athena-duckdb-functions-binary-sha256).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-sha1-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-binary-sha256"></a>

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
<a name="athena-duckdb-functions-binary-sha256-syntax"></a>

```sql
sha256({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-sha256-arguments"></a>

The `sha256` function takes the following argument:

**{{value}}**  
The value to hash, as a `VARCHAR` or `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-sha256-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-sha256-examples"></a>

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

```sql
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.

```sql
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.

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

The query returns the following result.


| hash\_bytes | 
| --- | 
| `32` | 

### Considerations
<a name="athena-duckdb-functions-binary-sha256-considerations"></a>

`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
<a name="athena-duckdb-functions-binary-sha256-differences-from-engine-version-3"></a>

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}}))`.