File: pysha3.py

package info (click to toggle)
python-eth-hash 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308 kB
  • sloc: python: 659; makefile: 233
file content (41 lines) | stat: -rw-r--r-- 960 bytes parent folder | download | duplicates (2)
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
from typing import (
    Union,
)

from sha3 import (
    keccak_256 as _keccak_256,
)

from eth_hash.abc import (
    BackendAPI,
    PreImageAPI,
)


class Pysha3Preimage(PreImageAPI):
    def __init__(self, prehash: bytes) -> None:
        self._hash = _keccak_256(prehash)

    def update(self, prehash: bytes) -> None:
        return self._hash.update(prehash)  # type: ignore

    def digest(self) -> bytes:
        return self._hash.digest()  # type: ignore

    def copy(self) -> "Pysha3Preimage":
        dup = Pysha3Preimage(b"")
        dup._hash = self._hash.copy()
        return dup


class PySha3Backend(BackendAPI):
    def keccak256(self, prehash: Union[bytearray, bytes]) -> bytes:
        return _keccak_256(prehash).digest()  # type: ignore

    def preimage(self, prehash: Union[bytearray, bytes]) -> PreImageAPI:
        return Pysha3Preimage(prehash)


backend = PySha3Backend()
keccak256 = backend.keccak256
preimage = backend.preimage