ethereum.crypto.hash

Cryptographic Hash Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

Introduction

Cryptographic hashing functions.

 1"""
 2Cryptographic Hash Functions
 3^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 4
 5.. contents:: Table of Contents
 6    :backlinks: none
 7    :local:
 8
 9Introduction
10------------
11
12Cryptographic hashing functions.
13"""
14
15from Crypto.Hash import keccak
16
17from ..base_types import Bytes, Bytes32, Bytes64
18
19Hash32 = Bytes32
20Hash64 = Bytes64
21
22
23def keccak256(buffer: Bytes) -> Hash32:
24    """
25    Computes the keccak256 hash of the input `buffer`.
26
27    Parameters
28    ----------
29    buffer :
30        Input for the hashing function.
31
32    Returns
33    -------
34    hash : `ethereum.base_types.Hash32`
35        Output of the hash function.
36    """
37    k = keccak.new(digest_bits=256)
38    return Hash32(k.update(buffer).digest())
39
40
41def keccak512(buffer: Bytes) -> Hash64:
42    """
43    Computes the keccak512 hash of the input `buffer`.
44
45    Parameters
46    ----------
47    buffer :
48        Input for the hashing function.
49
50    Returns
51    -------
52    hash : `ethereum.base_types.Hash32`
53        Output of the hash function.
54    """
55    k = keccak.new(digest_bits=512)
56    return Hash64(k.update(buffer).digest())
def keccak256(buffer: bytes) -> ethereum.base_types.Bytes32:
24def keccak256(buffer: Bytes) -> Hash32:
25    """
26    Computes the keccak256 hash of the input `buffer`.
27
28    Parameters
29    ----------
30    buffer :
31        Input for the hashing function.
32
33    Returns
34    -------
35    hash : `ethereum.base_types.Hash32`
36        Output of the hash function.
37    """
38    k = keccak.new(digest_bits=256)
39    return Hash32(k.update(buffer).digest())

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 keccak512(buffer: bytes) -> ethereum.base_types.Bytes64:
42def keccak512(buffer: Bytes) -> Hash64:
43    """
44    Computes the keccak512 hash of the input `buffer`.
45
46    Parameters
47    ----------
48    buffer :
49        Input for the hashing function.
50
51    Returns
52    -------
53    hash : `ethereum.base_types.Hash32`
54        Output of the hash function.
55    """
56    k = keccak.new(digest_bits=512)
57    return Hash64(k.update(buffer).digest())

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.