Skip to content

Commit

Permalink
Merge pull request #17 from carver/update-after-digest
Browse files Browse the repository at this point in the history
Update after digest
  • Loading branch information
carver committed May 14, 2018
2 parents 8ce7925 + a8e1e44 commit 504cb38
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion eth_hash/backends/pycryptodome.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from Crypto.Hash import (
keccak,
)

from eth_hash.preimage import (
BasePreImage,
)
Expand All @@ -15,7 +16,7 @@ class preimage(BasePreImage):
_hash = None

def __init__(self, prehash) -> None:
self._hash = keccak.new(data=prehash, digest_bits=256)
self._hash = keccak.new(data=prehash, digest_bits=256, update_after_digest=True)
# pycryptodome doesn't expose a `copy` mechanism for it's hash objects
# so we keep a record of all of the parts for when/if we need to copy
# them.
Expand Down
7 changes: 4 additions & 3 deletions eth_hash/backends/pysha3.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from eth_hash.preimage import (
BasePreImage,
)
from sha3 import (
keccak_256 as _keccak_256,
)

from eth_hash.preimage import (
BasePreImage,
)


def keccak256(prehash: bytes) -> bytes:
return _keccak_256(prehash).digest()
Expand Down
8 changes: 8 additions & 0 deletions tests/backends/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ def test_copy_keccak_256_preimage(keccak):

assert preimage_origin.digest() == b"\xb1\xf3T\xb2\x8f\xf2\x84R\xd6\xb9\xd6\x1fA\x06\x1b\xbe\x82\xbe\xb1\xfc\x98\xf33d\xa8\x05\x8d\x1a]\x16M\x05" # noqa: E501
assert preimage_copy.digest() == b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';\x7b\xfa\xd8\x04]\x85\xa4p" # noqa: E501


def test_update_digest_update(keccak):
preimage = keccak.new(b'')
preimage.update(b'arst')
assert preimage.digest() == b"\xc30[\xc9\xde\x12D\xe4\x80P\x96,\xedP\xb7Y4\xc3p\x06\xe9\x9e\x8bzb!>\x94\\=\xfc\xd7" # noqa: E501
preimage.update(b'tsra')
assert preimage.digest() == b"\xb1\xf3T\xb2\x8f\xf2\x84R\xd6\xb9\xd6\x1fA\x06\x1b\xbe\x82\xbe\xb1\xfc\x98\xf33d\xa8\x05\x8d\x1a]\x16M\x05" # noqa: E501
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ force_sort_within_sections=True
include_trailing_comma=True
known_standard_library=pytest
known_first_party=eth_hash
known_third_party=Crypto,sha3
line_length=21
multi_line_output=3
use_parentheses=True
Expand Down

0 comments on commit 504cb38

Please sign in to comment.