

# Hash functions
<a name="s_hash-functions"></a>

A hash function is a mathematical function that converts a numerical input value into another value. 

AWS Clean Rooms Spark SQL supports the following hash functions:

**Topics**
+ [MD5 function](s_MD5.md)
+ [SHA function](s_SHA.md)
+ [SHA1 function](s_SHA1.md)
+ [SHA2 function](s_SHA2.md)
+ [xxHASH64 function](xxhash64.md)

# MD5 function
<a name="s_MD5"></a>

Uses the MD5 cryptographic hash function to convert a variable-length string into a 32-character string that is a text representation of the hexadecimal value of a 128-bit checksum. 

## Syntax
<a name="s_MD5-syntax"></a>

```
MD5(string)
```

## Arguments
<a name="s_MD5-arguments"></a>

 *string*   
A variable-length string.

## Return type
<a name="s_MD5-return-type"></a>

The MD5 function returns a 32-character string that is a text representation of the hexadecimal value of a 128-bit checksum.

## Examples
<a name="s_MD5-examples"></a>

The following example shows the 128-bit value for the string 'AWS Clean Rooms': 

```
select md5('AWS Clean Rooms');
md5
----------------------------------
f7415e33f972c03abd4f3fed36748f7a
(1 row)
```

# SHA function
<a name="s_SHA"></a>

Synonym of SHA1 function. 

See [SHA1 function](s_SHA1.md). 

# SHA1 function
<a name="s_SHA1"></a>

The SHA1 function uses the SHA1 cryptographic hash function to convert a variable-length string into a 40-character string that is a text representation of the hexadecimal value of a 160-bit checksum.

## Syntax
<a name="s_SHA1-syntax"></a>

SHA1 is a synonym of [SHA function](s_SHA.md). 

```
SHA1(string)
```

## Arguments
<a name="s_SHA1-arguments"></a>

 *string*   
A variable-length string.

## Return type
<a name="s_SHA1-returm-type"></a>

The SHA1 function returns a 40-character string that is a text representation of the hexadecimal value of a 160-bit checksum. 

## Example
<a name="s_SHA1-example"></a>

The following example returns the 160-bit value for the word 'AWS Clean Rooms': 

```
select sha1('AWS Clean Rooms');
```

# SHA2 function
<a name="s_SHA2"></a>

The SHA2 function uses the SHA2 cryptographic hash function to convert a variable-length string into a character string. The character string is a text representation of the hexadecimal value of the checksum with the specified number of bits.

## Syntax
<a name="s_SHA2-syntax"></a>

```
SHA2(string, bits)
```

## Arguments
<a name="s_SHA2-arguments"></a>

 *string*   
A variable-length string.

 *integer*   
The number of bits in the hash functions. Valid values are 0 (same as 256), 224, 256, 384, and 512.

## Return type
<a name="s_SHA2-returm-type"></a>

The SHA2 function returns a character string that is a text representation of the hexadecimal value of the checksum or an empty string if the number of bits is invalid. 

## Example
<a name="s_SHA2-example"></a>

The following example returns the 256-bit value for the word 'AWS Clean Rooms': 

```
select sha2('AWS Clean Rooms', 256);
```

# xxHASH64 function
<a name="xxhash64"></a>

The xxhash64 function returns a 64-bit hash value of the arguments. 

The xxhash64() function is a non-cryptographic hash function designed to be fast and efficient. It's often used in data processing and storage applications, where a unique identifier for a piece of data is needed, but the exact contents of the data don't need to be kept secret. 

In the context of a SQL query, the xxhash64() function could be used for various purposes, such as: 
+ Generating a unique identifier for a row in a table 
+ Partitioning data based on a hash value 
+ Implementing custom indexing or data distribution strategies 

The specific use case would depend on the requirements of the application and the data being processed.

## Syntax
<a name="xxhash64-syntax"></a>

```
xxhash64(expr1, expr2, ...)
```

## Arguments
<a name="xxhash64-arguments"></a>

*expr1*  
An expression of any type.

*expr2*  
An expression of any type.

## Returns
<a name="xxhash64-returns"></a>

Returns a 64-bit hash value of the arguments (BIGINT). Hash seed is 42.

## Example
<a name="xxhash64-example"></a>

The following example generates a 64-bit hash value (5602566077635097486) based on the provided input. The first argument is a string value, in this case, the word "Spark". The second argument is an array containing the single integer value 123. The third argument is an integer value representing the seed for the hash function.

```
SELECT xxhash64('Spark', array(123), 2);
 5602566077635097486
```