forked from ethereum/execution-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.py
More file actions
57 lines (42 loc) · 1.08 KB
/
hash.py
File metadata and controls
57 lines (42 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Cryptographic Hash Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. contents:: Table of Contents
:backlinks: none
:local:
Introduction
------------
Cryptographic hashing functions.
"""
from Crypto.Hash import keccak
from ethereum_types.bytes import Bytes, Bytes32, Bytes64
Hash32 = Bytes32
Hash64 = Bytes64
def keccak256(buffer: Bytes | bytearray) -> Hash32:
"""
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.
"""
k = keccak.new(digest_bits=256)
return Hash32(k.update(buffer).digest())
def keccak512(buffer: Bytes | bytearray) -> Hash64:
"""
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.
"""
k = keccak.new(digest_bits=512)
return Hash64(k.update(buffer).digest())