Python Glossary Русский

Python 3.6+hashlibfunction

hashlib.shake_128()

hashing

The SHAKE128 hash from the SHA-3 family with an output of any length: you pass the length in bytes to digest(n) and hexdigest(n).

Syntax

hashlib.shake_128(data=b'', *, usedforsecurity=True).hexdigest(length)

hexdigest() without an argument raises TypeError — the length is required, and it counts bytes, not characters: hexdigest(8) gives 16 hex digits. A shorter output is a prefix of a longer one, so hexdigest(8) matches the start of hexdigest(16). Security is capped at 128 bits however many bytes you ask for; need more — use shake_256().

Examples

import hashlib
h = hashlib.shake_128(b'abc')
print(h.hexdigest(8))  # → 5881092dd818bf5c
print(h.hexdigest(16)[:16])  # → 5881092dd818bf5c — короткий выход — префикс
# Python 3.9+
import hashlib
try:
    hashlib.shake_128(b'abc').hexdigest()
except TypeError as e:
    print(e)  # → hexdigest() missing required argument 'length' (pos 1)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy