Python Glossary Русский

Python 3.6+hashlibfunction

hashlib.sha3_256()

hashing

The SHA3-256 hash (SHA-3 family, Keccak): 32 bytes, 64 hex digits. Same length as SHA-256, but a different algorithm.

Syntax

hashlib.sha3_256(data=b'', *, usedforsecurity=True)

SHA-3 is built differently from SHA-2 (a sponge instead of the Merkle-Damgard construction), so sha3_256() and sha256() give different values, and SHA-3 has no length-extension attack. A trap when matching other systems: Ethereum and some libraries call the original Keccak "keccak256", which is not the standardized SHA3-256 — the hashes will not match. This is the usual pick when SHA-3 is what you need.

Examples

import hashlib
h = hashlib.sha3_256(b'abc')
print(h.hexdigest()[:16])  # → 3a985da74fe225b2
print(h.digest_size, h.block_size)  # → 32 136
import hashlib
a = hashlib.sha3_256(b'abc').hexdigest()
b = hashlib.sha256(b'abc').hexdigest()
print(len(a) == len(b), a == b)  # → True False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy