ethereum.crypto.hash

Cryptographic Hash Functions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Cryptographic hashing functions.

Hash32

19
Hash32 = Bytes32

Hash64

20
Hash64 = Bytes64

_hashlib_has_keccak

Return True if hashlib can compute pre-NIST Keccak digests.

def _hashlib_has_keccak() -> bool:
24
    <snip>
25
    try:
26
        hashlib.new("keccak-256", b"")
27
    except ValueError:
28
        return False
29
    return True

_USE_HASHLIB

39
_USE_HASHLIB = _hashlib_has_keccak()

def _keccak256_digest(buffer: bytes | bytearray) -> bytes:
45
        return hashlib.new("keccak-256", buffer).digest()

def _keccak512_digest(buffer: bytes | bytearray) -> bytes:
48
        return hashlib.new("keccak-512", buffer).digest()

def _keccak256_digest(buffer: bytes | bytearray) -> bytes:
52
        return (
53
            _pycryptodome_keccak.new(digest_bits=256).update(buffer).digest()
54
        )

def _keccak512_digest(buffer: bytes | bytearray) -> bytes:
57
        return (
58
            _pycryptodome_keccak.new(digest_bits=512).update(buffer).digest()
59
        )

keccak256

Computes the keccak256 hash of the input buffer.

Parameters

buffer : Input for the hashing function.

Returns

hash : ethereum.base_types.Hash32 Output of the hash function.

def keccak256(buffer: bytes | bytearray) -> Hash32:
63
    <snip>
77
    return Hash32((buffer))

keccak512

Computes the keccak512 hash of the input buffer.

Parameters

buffer : Input for the hashing function.

Returns

hash : ethereum.base_types.Hash32 Output of the hash function.

def keccak512(buffer: bytes | bytearray) -> Hash64:
81
    <snip>
95
    return Hash64((buffer))