Skip to content

Commit

Permalink
Add comprehensive tests of lib/hash.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Booth committed Jul 10, 2017
1 parent 858bac2 commit 197d36b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/hash.py
Expand Up @@ -34,13 +34,11 @@

def sha256(x):
'''Simple wrapper of hashlib sha256.'''
assert isinstance(x, (bytes, bytearray, memoryview))
return hashlib.sha256(x).digest()


def ripemd160(x):
'''Simple wrapper of hashlib ripemd160.'''
assert isinstance(x, (bytes, bytearray, memoryview))
h = hashlib.new('ripemd160')
h.update(x)
return h.digest()
Expand All @@ -63,13 +61,15 @@ def hash160(x):
return ripemd160(sha256(x))


def hash_to_str(x):
def hash_to_hex_str(x):
'''Convert a big-endian binary hash to displayed hex string.
Display form of a binary hash is reversed and converted to hex.
'''
return bytes(reversed(x)).hex()

# Temporary
hash_to_str = hash_to_hex_str

def hex_str_to_hash(x):
'''Convert a displayed hex string to a binary hash.'''
Expand Down Expand Up @@ -98,7 +98,7 @@ def char_value(c):
def decode(txt):
"""Decodes txt into a big-endian bytearray."""
if not isinstance(txt, str):
raise Base58Error('a string is required')
raise TypeError('a string is required')

if not txt:
raise Base58Error('string cannot be empty')
Expand Down Expand Up @@ -151,7 +151,5 @@ def decode_check(txt):
def encode_check(payload):
"""Encodes a payload bytearray (which includes the version byte(s))
into a Base58Check string."""
assert isinstance(payload, (bytes, bytearray, memoryview))

be_bytes = payload + double_sha256(payload)[:4]
return Base58.encode(be_bytes)
68 changes: 68 additions & 0 deletions tests/lib/test_hash.py
@@ -0,0 +1,68 @@
#
# Tests of lib/hash.py
#

import pytest

import lib.hash as lib_hash


def test_sha256():
assert lib_hash.sha256(b'sha256') == b'][\t\xf6\xdc\xb2\xd5:_\xff\xc6\x0cJ\xc0\xd5_\xab\xdfU`i\xd6c\x15E\xf4*\xa6\xe3P\x0f.'
with pytest.raises(TypeError):
lib_hash.sha256('sha256')

def ripemd160(x):
assert lib_hash.ripemd160(b'ripemd160') == b'\x903\x91\xa1\xc0I\x9e\xc8\xdf\xb5\x1aSK\xa5VW\xf9|W\xd5'
with pytest.raises(TypeError):
lib_hash.ripemd160('ripemd160')

def test_double_sha256():
assert lib_hash.double_sha256(b'double_sha256') == b'ksn\x8e\xb7\xb9\x0f\xf6\xd9\xad\x88\xd9#\xa1\xbcU(j1Bx\xce\xd5;s\xectL\xe7\xc5\xb4\x00'

def test_hmac_sha512():
assert lib_hash.hmac_sha512(b'key', b'message') == b"\xe4w8M|\xa2)\xdd\x14&\xe6Kc\xeb\xf2\xd3n\xbdm~f\x9ag5BNr\xeal\x01\xd3\xf8\xb5n\xb3\x9c6\xd8#/T'\x99\x9b\x8d\x1a?\x9c\xd1\x12\x8f\xc6\x9fMu\xb44!h\x10\xfa6~\x98"

def test_hash160():
assert lib_hash.hash160(b'hash_160') == b'\xb3\x96\x94\xfc\x978R\xa7)XqY\xbb\xdc\xeb\xac\xa7%\xb8$'

def test_hash_to_hex_str():
assert lib_hash.hash_to_hex_str(b'hash_to_str') == '7274735f6f745f68736168'

def test_hex_str_to_hash():
assert lib_hash.hex_str_to_hash('7274735f6f745f68736168') == b'hash_to_str'

def test_Base58_char_value():
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
for value, c in enumerate(chars):
assert lib_hash.Base58.char_value(c) == value
for c in (' ', 'I', '0', 'l', 'O'):
with pytest.raises(lib_hash.Base58Error):
lib_hash.Base58.char_value(c)

def test_Base58_decode():
with pytest.raises(TypeError):
lib_hash.Base58.decode(b'foo')
with pytest.raises(lib_hash.Base58Error):
lib_hash.Base58.decode('')
assert lib_hash.Base58.decode('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz') == b'\x00\x01\x11\xd3\x8e_\xc9\x07\x1f\xfc\xd2\x0bJv<\xc9\xaeO%+\xb4\xe4\x8f\xd6j\x83^%*\xda\x93\xffH\rm\xd4=\xc6*d\x11U\xa5'
assert lib_hash.Base58.decode('3i37NcgooY8f1S') == b'0123456789'

def test_Base58_encode():
with pytest.raises(TypeError):
lib_hash.Base58.encode('foo')
assert lib_hash.Base58.encode(b'') == ''
assert lib_hash.Base58.encode(b'\0') == '1'
assert lib_hash.Base58.encode(b'0123456789') == '3i37NcgooY8f1S'

def test_Base58_decode_check():
with pytest.raises(TypeError):
lib_hash.Base58.decode_check(b'foo')
assert lib_hash.Base58.decode_check('4t9WKfuAB8') == b'foo'
with pytest.raises(lib_hash.Base58Error):
lib_hash.Base58.decode_check('4t9WKfuAB9')

def test_Base58_encode_check():
with pytest.raises(TypeError):
lib_hash.Base58.encode_check('foo')
assert lib_hash.Base58.encode_check(b'foo') == '4t9WKfuAB8'

0 comments on commit 197d36b

Please sign in to comment.