Skip to content
Snippets Groups Projects
Verified Commit 8776435d authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

swh.model.hashutil: Simplify length hash algorithms instantiation

The same caveat applies, will only be supported from python3.6 onward.

Related T692
parent 9c25f8f5
No related branches found
No related tags found
No related merge requests found
......@@ -84,14 +84,7 @@ def _new_hash(algo, length=None):
base_algo = _algo[0]
variable_length = int(_algo[1])
if base_algo == 'blake2b':
h = hashlib.blake2b(digest_size=variable_length)
elif base_algo == 'blake2s':
h = hashlib.blake2s(digest_size=variable_length)
else:
raise ValueError('Unexpected hashing algorithm %s, '
'expected one of %s' %
(algo, ', '.join(sorted(ALGORITHMS))))
h = hashlib.new('%s%s' % (base_algo, variable_length))
else:
raise ValueError('Unsupported hashing algorithm %s' % algo)
else:
......
......@@ -142,32 +142,32 @@ class Hashutil(unittest.TestCase):
hashutil._new_hash('blake3:256')
except ValueError as e:
self.assertEquals(str(e),
'Unexpected hashing algorithm blake3:256, '
'expected one of sha1, sha1_git, sha256')
'unsupported hash type blake3256')
@patch('swh.model.hashutil.sys')
@patch('swh.model.hashutil.hashlib')
@istest
def new_hash_blake2b(self, mock_hashlib, mock_sys):
mock_sys.version_info = MagicMock(major=3, minor=6)
mock_hashlib.blake2b.return_value = 'some-hashlib-object'
mock_hashlib.new.return_value = 'some-hashlib-object'
h = hashutil._new_hash('blake2b:256')
h = hashutil._new_hash('blake2b:512')
self.assertEquals(h, 'some-hashlib-object')
mock_hashlib.blake2b.assert_called_with(digest_size=256)
mock_hashlib.new.assert_called_with('blake2b512')
@patch('swh.model.hashutil.sys')
@patch('swh.model.hashutil.hashlib')
@istest
def new_hash_blake2s(self, mock_hashlib, mock_sys):
mock_sys.version_info = MagicMock(major=3, minor=6)
mock_hashlib.blake2s.return_value = 'some-hashlib-object'
mock_hashlib.new.return_value = 'some-hashlib-object'
h = hashutil._new_hash('blake2s:128')
h = hashutil._new_hash('blake2s:256')
self.assertEquals(h, 'some-hashlib-object')
mock_hashlib.blake2s.assert_called_with(digest_size=128)
mock_hashlib.new.assert_called_with('blake2s256')
class HashlibGit(unittest.TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment