chash — Cryptographic hash functions¶
Cryptographic hash functions, on pl.Expr as .chash.
Every expression here accepts Utf8 or Binary and gives a lowercase hexadecimal string. A digest reads bytes, so the data type of the input does not change the result. A null input gives a null output.
sha2_256
¶
sha2_512
¶
sha2_384
¶
sha2_224
¶
sha3_256
¶
SHA3-256 from the SHA-3 family.
The digest has the size of sha2_256()
and a different construction. The two expressions do not give the same value.
Returns:
| Type | Description |
|---|---|
Expr
|
Utf8 with 64 characters. |
Examples:
sha3_512
¶
sha3_384
¶
sha3_224
¶
sha3_shake128
¶
SHAKE128, the extendable-output function from the SHA-3 family.
Every other digest in this namespace has a fixed size. Here you set the size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
The digest size in bytes. The hexadecimal output has two
characters for each byte. A |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
Utf8 with |
Note
A short output is the start of a longer output for the same input.
If you cut a long digest to n bytes, you get the digest that
length=n gives.
Examples:
blake3
¶
BLAKE3 with the default 256-bit output.
BLAKE3 is much faster than SHA-2. Use it for large quantities of data.
Returns:
| Type | Description |
|---|---|
Expr
|
Utf8 with 64 characters. |
Examples:
>>> df = pl.DataFrame({"foo": ["hello_world"]})
>>> df.select(plh.col("foo").chash.blake3()).item()
'9833e5324eb2400de814730f4e92810905351bc0451e10b75847210c1d7c37ed'
Each expression in this namespace also hashes the bytes of a Binary column:
hmac_sha256
¶
Keyed HMAC-SHA256 (RFC 2104).
The digest is a function of the input and the key. One input with two different keys gives two different digests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key. It can have any length, and an empty key is permitted. polars-hash expands the key one time for each expression, not one time for each row. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
Utf8 with 64 characters. |
Warning
polars-hash writes key into the keyword arguments of the
expression. The key therefore appears in the output of explain()
and in each plan that you cache or write to a log.
Examples: