Hash functions

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/functions/hash-functions.md)

Hash functions can be used for the deterministic pseudo-random shuffling of elements.

Simhash is a hash function, which returns close hash values for close (similar) arguments.

Most hash functions accept any number of arguments of any types.

Note

Hash of NULL is NULL. To get a non-NULL hash of a Nullable column, wrap it in a tuple:

```
SELECT cityHash64(tuple(NULL))
```

Note

To calculate hash of the whole contents of a table, use `sum(cityHash64(tuple(*)))` (or other hash function). `tuple` ensures that rows with NULL values are not skipped. `sum` ensures that the order of rows doesn't matter.

## BLAKE3[​](#BLAKE3 "Direct link to BLAKE3")

Introduced in: v22.10.0

Calculates BLAKE3 hash string and returns the resulting set of bytes as FixedString.
This cryptographic hash-function is integrated into ClickHouse with BLAKE3 Rust library.
The function is rather fast and shows approximately two times faster performance compared to SHA-2, while generating hashes of the same length as SHA-256.
It returns a BLAKE3 hash as a byte array with type FixedString(32).

**Syntax**

```
BLAKE3(message)
```

**Arguments**

- `message` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the 32-byte BLAKE3 hash of the input string as a fixed-length string. [`FixedString(32)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**hash**

```
SELECT hex(BLAKE3('ABC'))
```

```
┌─hex(BLAKE3('ABC'))───────────────────────────────────────────────┐
│ D1717274597CF0289694F75D96D444B992A096F1AFD8E7BBFA6EBB1D360FEDFC │
└──────────────────────────────────────────────────────────────────┘
```

## MD4[​](#MD4 "Direct link to MD4")

Introduced in: v21.11.0

Calculates the MD4 hash of the given string.

**Syntax**

```
MD4(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the MD4 hash of the given input string as a fixed-length string. [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(MD4('abc'));
```

```
┌─hex(MD4('abc'))──────────────────┐
│ A448017AAF21D8525FC10AE87AA6729D │
└──────────────────────────────────┘
```

## MD5[​](#MD5 "Direct link to MD5")

Introduced in: v1.1.0

Calculates the MD5 hash of the given string.

**Syntax**

```
MD5(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the MD5 hash of the given input string as a fixed-length string. [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(MD5('abc'));
```

```
┌─hex(MD5('abc'))──────────────────┐
│ 900150983CD24FB0D6963F7D28E17F72 │
└──────────────────────────────────┘
```

## RIPEMD160[​](#RIPEMD160 "Direct link to RIPEMD160")

Introduced in: v24.10.0

Calculates the RIPEMD-160 hash of the given string.

**Syntax**

```
RIPEMD160(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the RIPEMD160 hash of the given input string as a fixed-length string. [`FixedString(20)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(RIPEMD160('The quick brown fox jumps over the lazy dog'));
```

```
┌─HEX(RIPEMD160('The quick brown fox jumps over the lazy dog'))─┐
│ 37F332F68DB77BD9D7EDD4969571AD671CF9DD3B                      │
└───────────────────────────────────────────────────────────────┘
```

## SHA1[​](#SHA1 "Direct link to SHA1")

Introduced in: v1.1.0

Calculates the SHA1 hash of the given string.

**Syntax**

```
SHA1(s)
```

**Arguments**

- `s` — The input string to hash [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA1 hash of the given input string as a fixed-length string. [`FixedString(20)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA1('abc'));
```

```
┌─hex(SHA1('abc'))─────────────────────────┐
│ A9993E364706816ABA3E25717850C26C9CD0D89D │
└──────────────────────────────────────────┘
```

## SHA224[​](#SHA224 "Direct link to SHA224")

Introduced in: v1.1.0

Calculates the SHA224 hash of the given string.

**Syntax**

```
SHA224(s)
```

**Arguments**

- `s` — The input value to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA224 hash of the given input string as a fixed-length string. [`FixedString(28)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA224('abc'));
```

```
┌─hex(SHA224('abc'))───────────────────────────────────────┐
│ 23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7 │
└──────────────────────────────────────────────────────────┘
```

## SHA256[​](#SHA256 "Direct link to SHA256")

Introduced in: v1.1.0

Calculates the SHA256 hash of the given string.

**Syntax**

```
SHA256(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA256 hash of the given input string as a fixed-length string. [`FixedString(32)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA256('abc'));
```

```
┌─hex(SHA256('abc'))───────────────────────────────────────────────┐
│ BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD │
└──────────────────────────────────────────────────────────────────┘
```

## SHA384[​](#SHA384 "Direct link to SHA384")

Introduced in: v1.1.0

Calculates the SHA384 hash of the given string.

**Syntax**

```
SHA384(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA384 hash of the given input string as a fixed-length string. [`FixedString(48)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA384('abc'));
```

```
┌─hex(SHA384('abc'))───────────────────────────────────────────────────────────────────────────────┐
│ CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```

## SHA512[​](#SHA512 "Direct link to SHA512")

Introduced in: v1.1.0

Calculates the SHA512 hash of the given string.

**Syntax**

```
SHA512(s)
```

**Arguments**

- `s` — The input string to hash [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA512 hash of the given input string as a fixed-length string. [`FixedString(64)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA512('abc'));
```

```
┌─hex(SHA512('abc'))───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

## SHA512\_256[​](#SHA512_256 "Direct link to SHA512_256")

Introduced in: v1.1.0

Calculates the SHA512\_256 hash of the given string.

**Syntax**

```
SHA512_256(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA512\_256 hash of the given input string as a fixed-length string. [`FixedString(32)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA512_256('abc'));
```

```
┌─hex(SHA512_256('abc'))───────────────────────────────────────────┐
│ 53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23 │
└──────────────────────────────────────────────────────────────────┘
```

## URLHash[​](#URLHash "Direct link to URLHash")

Introduced in: v1.1.0