Skip to content

Commit d15e9be

Browse files
committed
Python: Support extended hash identifiers
1 parent 22468a1 commit d15e9be

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

bindings/python/rhash/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
SHA3_512,
4545
BLAKE2S,
4646
BLAKE2B,
47+
BLAKE3,
4748
SNEFRU128,
4849
SNEFRU256,
4950
RHash,
@@ -91,6 +92,7 @@
9192
"SHA3_512",
9293
"BLAKE2S",
9394
"BLAKE2B",
95+
"BLAKE3",
9496
"SNEFRU128",
9597
"SNEFRU256",
9698
"RHash",

bindings/python/rhash/rhash.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
c_int,
7676
c_size_t,
7777
c_uint,
78+
c_ulonglong,
7879
c_void_p,
7980
create_string_buffer,
8081
)
@@ -126,6 +127,13 @@ def _run_rhash_ctrl(ctx, command, count=0, data=0):
126127
return _LIBRHASH.rhash_transmit(command, ctx, count, data)
127128

128129

130+
_HAS_RHASH_UPDATE_FD = hasattr(_LIBRHASH, "rhash_update_fd")
131+
_HAS_EXTENDED_HASH_IDS = _HAS_RHASH_UPDATE_FD
132+
if _HAS_RHASH_UPDATE_FD:
133+
_LIBRHASH.rhash_update_fd.argtypes = [c_void_p, c_int, c_ulonglong]
134+
_LIBRHASH.rhash_ctrl.restype = c_int
135+
136+
129137
# conversion of a string to binary data with Python 2/3 compatibility
130138
if sys.version < "3":
131139

@@ -187,7 +195,15 @@ def _msg_to_bytes(msg):
187195
SNEFRU256 = 0x10000000
188196
BLAKE2S = 0x20000000
189197
BLAKE2B = 0x40000000
190-
ALL = 0x7FFFFFFF
198+
BLAKE3 = 0x8000001F
199+
if _HAS_EXTENDED_HASH_IDS:
200+
ALL = 0xFF000000
201+
else:
202+
ALL = 0x7FFFFFFF
203+
204+
_LOW_HASH_MASK = 0x7FFFFFFF
205+
_EXTENDED_BIT = 0x80000000
206+
_MAX_HASH_COUNT = 256
191207

192208
# four deprecated constants
193209
SHA224 = 0x10000
@@ -259,7 +275,14 @@ def _are_good_ids(hash_ids):
259275
for alg_id in hash_ids:
260276
if not isinstance(alg_id, int) or alg_id <= 0:
261277
raise InvalidArgumentError("Invalid value of hash_ids")
262-
if (alg_id & ALL) != alg_id or ((alg_id - 1) & alg_id) != 0:
278+
if (alg_id & _EXTENDED_BIT) == 0:
279+
if ((alg_id - 1) & alg_id) != 0:
280+
return False
281+
elif not _HAS_EXTENDED_HASH_IDS:
282+
raise InvalidArgumentError(
283+
"Hash_id is not supported by the installed librhash version"
284+
)
285+
elif (alg_id & _LOW_HASH_MASK) > _MAX_HASH_COUNT:
263286
return False
264287
return True
265288

@@ -307,7 +330,7 @@ def _print(self, hash_id, flags):
307330
size = _LIBRHASH.rhash_print(buf, self._ctx, hash_id, flags)
308331
if not size:
309332
raise InvalidArgumentError(
310-
"Requested message digest for an invalid hash_id = {}".format(hash_id)
333+
"Requested message digest for an unsupported hash_id = {}".format(hash_id)
311334
)
312335
if (flags & 3) == _RHPR_RAW:
313336
return buf[0:size]

bindings/python/setup.cfg

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = rhash-Rhash
3-
version = 1.2
3+
version = 1.3
44
author = Aleksey Kravchenko
55
author_email = [email protected]
66
description = Python interface for librhash
@@ -10,10 +10,15 @@ url = https://github.com/rhash/RHash
1010
project_urls =
1111
Source = https://github.com/rhash/RHash
1212
Bug Tracker = https://github.com/rhash/RHash/issues
13-
license = BSD Zero Clause License
13+
Documentation = https://github.com/rhash/RHash/tree/master/bindings/python#readme
14+
license = 0BSD
15+
license_files = LICENSE
1416
classifiers =
17+
Development Status :: 5 - Production/Stable
18+
Intended Audience :: Developers
19+
Intended Audience :: Information Technology
20+
Intended Audience :: System Administrators
1521
Programming Language :: Python :: 3
16-
License :: OSI Approved :: BSD License
1722
Operating System :: OS Independent
1823

1924
[options]

bindings/python/tests/rhash_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def test_all_hashes(self):
128128
"d5abe2c6b1b5ee3face62fed78dbef802f2a85cb91d455a8f5249d330853cb3c",
129129
ctx.hash(rhash.BLAKE2B),
130130
)
131+
self.assertEqual(
132+
"17762fddd969a453925d65717ac3eea21320b66b54342fde15128d6caf21215f",
133+
ctx.hash(rhash.BLAKE3),
134+
)
131135
# Test reset
132136
ctx.reset().finish()
133137
# Verify MD5( "" )

0 commit comments

Comments
 (0)