<a id="flink-sql-hash-functions"></a>

# Hash Functions in Confluent Cloud for Apache Flink

Confluent Cloud for Apache Flink® provides these built-in functions to generate hash codes in SQL
queries:

- [MD5](#flink-sql-md5-function)
- [SHA1](#flink-sql-sha1-function)
- [SHA2](#flink-sql-sha2-function)
- [SHA224](#flink-sql-sha224-function)
- [SHA256](#flink-sql-sha256-function)
- [SHA384](#flink-sql-sha384-function)
- [SHA512](#flink-sql-sha512-function)

<a id="flink-sql-md5-function"></a>

## MD5

Gets the MD5 hash of a string.

Syntax
: ```sql
  MD5(string)
  ```

Description
: The `MD5` function returns the MD5 hash of the specified string as a string
  of *32* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns 99dc0ea422440e5b3f675cffe6d...
  SELECT MD5('string-to-hash');
  ```

<a id="flink-sql-sha1-function"></a>

## SHA1

Gets the SHA-1 hash of a string.

Syntax
: ```sql
  SHA1(string)
  ```

Description
: The `SHA1` function returns the SHA-1 hash of the specified string as a
  string of *40* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns 771a2b04044f8c51e3383a2675a...
  SELECT SHA1('string-to-hash');
  ```

<a id="flink-sql-sha2-function"></a>

## SHA2

Hashes a string with one of the SHA-2 functions.

Syntax
: ```sql
  SHA2(string, hashLength)
  ```

Description
: The `SHA2` function returns the hash using the SHA-2 family of hash functions
  (SHA-224, SHA-256, SHA-384, and SHA-512).
  <br/>
  - The first argument, `string`, is the string to be hashed.
  - The second argument, `hashLength`, is the bit length of the result.
  <br/>
  These are the valid bit lengths for `hashLength`:
  <br/>
  - *224*
  - *256*
  - *384*
  - *512*
  <br/>
  Returns NULL if `string` or `hashLength` is NULL.

Example
: ```sql
  -- returns 222145560dbaa2abc1617e2c7ce...
  SELECT SHA2('string-to-hash', 512);
  ```

<a id="flink-sql-sha224-function"></a>

## SHA224

Gets the SHA-224 hash of a string.

Syntax
: ```sql
  SHA224(string)
  ```

Description
: The `SHA224` function returns the SHA-224 hash of the specified string as a
  string of *56* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns af1f1c988d9154f2ddb6201f60f...
  SELECT SHA224('string-to-hash');
  ```

<a id="flink-sql-sha256-function"></a>

## SHA256

Gets the SHA-256 hash of a string.

Syntax
: ```sql
  SHA256(string)
  ```

Description
: The `SHA256` function returns the SHA-256 hash of the specified string as a
  string of *64* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns 2267d414e45335fd02e64057d55...
  SELECT SHA256('string-to-hash');
  ```

<a id="flink-sql-sha384-function"></a>

## SHA384

Gets the SHA-384 hash of a string.

Syntax
: ```sql
  SHA384(string)
  ```

Description
: The `SHA384` function returns the SHA-384 hash of the specified string as a
  string of *96* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns 02ba979b23f1b4a098732463ea8...
  SELECT SHA384('string-to-hash');
  ```

<a id="flink-sql-sha512-function"></a>

## SHA512

Gets the SHA-512 hash of a string.

Syntax
: ```sql
  SHA512(string)
  ```

Description
: The `SHA512` function returns the SHA-512 hash of the specified string as a
  string of *128* hexadecimal digits.
  <br/>
  Returns NULL if `string` is NULL.

Example
: ```sql
  -- returns 222145560dbaa2abc1617e2c7ce...
  SELECT SHA512('string-to-hash');
  ```

## Other built-in functions

- [Aggregate Functions](aggregate-functions.md#flink-sql-aggregate-functions)
- [Changelog Conversion Functions](changelog-conversion.md#flink-sql-changelog-conversion-functions)
- [Collection Functions](collection-functions.md#flink-sql-collection-functions)
- [Comparison Functions](comparison-functions.md#flink-sql-comparison-functions)
- [Conditional Functions](conditional-functions.md#flink-sql-conditional-functions)
- [Datetime Functions](datetime-functions.md#flink-sql-datetime-functions)
- [Hash Functions](#flink-sql-hash-functions)
- [JSON Functions](json-functions.md#flink-sql-json-functions)
- [ML Preprocessing Functions](ml-preprocessing-functions.md#flink-sql-ml-preprocessing-functions)
- [Model Inference Functions](model-inference-functions.md#flink-sql-model-inference-functions)
- [Numeric Functions](numeric-functions.md#flink-sql-numeric-functions)
- [Search Functions](search-functions.md#flink-sql-search-functions)
- [String Functions](string-functions.md#flink-sql-string-functions)
- [Table API Functions](table-api-functions.md#flink-table-api-functions)

## Related content

- [User-defined Functions](../../concepts/user-defined-functions.md#flink-sql-udfs)
- [Create a User Defined Function](../../how-to-guides/create-udf.md#flink-sql-create-udf)

#### NOTE
This website includes content developed at the [Apache Software Foundation](https://www.apache.org/)
under the terms of the [Apache License v2](https://www.apache.org/licenses/LICENSE-2.0.html).
