Skip to content

Commit

Permalink
Support hashing of bytearrays (#5)
Browse files Browse the repository at this point in the history
* Support hashing of bytearrays

* Convert hashing arguments to bytes
  • Loading branch information
palango authored and pipermerriam committed Feb 28, 2018
1 parent 1fc4879 commit dca9098
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
22 changes: 14 additions & 8 deletions eth_hash/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ def __init__(self, backend):
assert self.hasher(b'') == 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 __call__(self, preimage):
if not isinstance(preimage, bytes):
raise TypeError("Can only compute the hash of a `bytes` value, not %r" % preimage)
if not isinstance(preimage, (bytes, bytearray)):
raise TypeError(
"Can only compute the hash of `bytes` or `bytearray` values, not %r" % preimage
)

return self.hasher(preimage)
return self.hasher(bytes(preimage))

def new(self, part):
if not isinstance(part, bytes):
raise TypeError("Can only compute the hash of a `bytes` value, not %r" % part)
if not isinstance(part, (bytes, bytearray)):
raise TypeError(
"Can only compute the hash of `bytes` or `bytearray` values, not %r" % part
)
return PreImage(part, keccak_fn=self.hasher)


Expand All @@ -22,9 +26,11 @@ def __init__(self, *parts, keccak_fn):
self.keccak_fn = keccak_fn

def update(self, part):
if not isinstance(part, bytes):
raise TypeError("Can only compute the hash of a `bytes` value, not %r" % part)
self.preimage_parts.append(part)
if not isinstance(part, (bytes, bytearray)):
raise TypeError(
"Can only compute the hash of `bytes` or `bytearray` values, not %r" % part
)
self.preimage_parts.append(bytes(part))

def digest(self):
return self.keccak_fn(b''.join(self.preimage_parts))
Expand Down
24 changes: 24 additions & 0 deletions tests/backends/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def keccak_auto():
b'',
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
),
(
bytearray(b''),
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_keccak_256(keccak, prehash, expected_result):
Expand All @@ -31,14 +35,34 @@ def test_keccak_256(keccak, prehash, expected_result):
[b''],
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
),
(
[bytearray(b'')],
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
),
(
[b'', b'', b''],
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
),
(
[bytearray(b''), bytearray(b''), bytearray(b'')],
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
),
(
[b'arst', b'tsra'],
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
),
(
[bytearray(b'arst'), bytearray(b'tsra')],
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
),
(
[bytearray(b'arst'), b'tsra'],
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
),
(
[b'arst', bytearray(b'tsra')],
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
),
),
)
def test_keccak_256_preimage(keccak, parts, expected_result):
Expand Down

0 comments on commit dca9098

Please sign in to comment.