Skip to content

Commit c59036e

Browse files
schlenkMichael Schlenkerjkowalleck
authored
feat: HashType.from_composite_str for Blake2b, SHA3, Blake3 (#663)
The code mistreated hashes for Blake2b and SHA3. Code for explicitly handling SHA1 & BLAKE3 was added, as those have no variants defined in the CycloneDX specification. fixes #652 --------- Signed-off-by: Michael Schlenker <[email protected]> Co-authored-by: Michael Schlenker <[email protected]> Co-authored-by: Jan Kowalleck <[email protected]>
1 parent 99bcdd2 commit c59036e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

cyclonedx/model/__init__.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ def from_composite_str(composite_hash: str) -> 'HashType':
418418
Composite Hash string of the format `HASH_ALGORITHM`:`HASH_VALUE`.
419419
Example: `sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b`.
420420
421+
Valid case insensitive prefixes are:
422+
`md5`, `sha1`, `sha256`, `sha384`, `sha512`, `blake2b256`, `blake2b384`, `blake2b512`,
423+
`blake2256`, `blake2384`, `blake2512`, `sha3-256`, `sha3-384`, `sha3-512`,
424+
`blake3`.
425+
421426
Raises:
422427
`UnknownHashTypeException` if the type of hash cannot be determined.
423428
@@ -432,17 +437,37 @@ def from_composite_str(composite_hash: str) -> 'HashType':
432437
alg=HashAlgorithm.MD5,
433438
content=parts[1].lower()
434439
)
440+
elif algorithm_prefix[0:4] == 'sha3':
441+
return HashType(
442+
alg=getattr(HashAlgorithm, f'SHA3_{algorithm_prefix[5:]}'),
443+
content=parts[1].lower()
444+
)
445+
elif algorithm_prefix == 'sha1':
446+
return HashType(
447+
alg=HashAlgorithm.SHA_1,
448+
content=parts[1].lower()
449+
)
435450
elif algorithm_prefix[0:3] == 'sha':
451+
# This is actually SHA2...
436452
return HashType(
437453
alg=getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}'),
438454
content=parts[1].lower()
439455
)
456+
elif algorithm_prefix[0:7] == 'blake2b':
457+
return HashType(
458+
alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[7:]}'),
459+
content=parts[1].lower()
460+
)
440461
elif algorithm_prefix[0:6] == 'blake2':
441462
return HashType(
442-
alg=getattr(HashAlgorithm, f'BLAKE2b_{algorithm_prefix[6:]}'),
463+
alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[6:]}'),
464+
content=parts[1].lower()
465+
)
466+
elif algorithm_prefix[0:6] == 'blake3':
467+
return HashType(
468+
alg=HashAlgorithm.BLAKE3,
443469
content=parts[1].lower()
444470
)
445-
446471
raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}')
447472

448473
def __init__(

tests/test_model.py

+17
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,23 @@ def test_hash_type_from_hashlib_alg_throws_on_unknown(self) -> None:
262262
HashAlgorithm.SHA_256, '806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b'),
263263
('MD5', 'MD5:dc26cd71b80d6757139f38156a43c545',
264264
HashAlgorithm.MD5, 'dc26cd71b80d6757139f38156a43c545'),
265+
('sha3-256', 'sha3-256:f43909a5e6420ee26b710718f296c7be85ba393e6b218107811067f49ea80101',
266+
HashAlgorithm.SHA3_256, 'f43909a5e6420ee26b710718f296c7be85ba393e6b218107811067f49ea80101'),
267+
('sha1', 'sha1:b82b9f695a3ae28053cb3776d2132ab625798055',
268+
HashAlgorithm.SHA_1, 'b82b9f695a3ae28053cb3776d2132ab625798055'),
269+
# Name format as used by 'openssl dgst and the Blake2 RFC'
270+
('blake2b512',
271+
'blake2b512:6d518ac5c7a022e954ecb21b8bf68d7f5c52e3c3579cd96f3bde4'
272+
'f76daaaa69a96a5eee268fb8fa2745930c37f0672424136b538878474bc4f586a63e13ae23f',
273+
HashAlgorithm.BLAKE2B_512,
274+
'6d518ac5c7a022e954ecb21b8bf68d7f5c52e3c3579cd96f3bde4f76daaaa69a'
275+
'96a5eee268fb8fa2745930c37f0672424136b538878474bc4f586a63e13ae23f'),
276+
('blake2512',
277+
'blake2512:6d518ac5c7a022e954ecb21b8bf68d7f5c52e3c3579cd96f3bde4'
278+
'f76daaaa69a96a5eee268fb8fa2745930c37f0672424136b538878474bc4f586a63e13ae23f',
279+
HashAlgorithm.BLAKE2B_512,
280+
'6d518ac5c7a022e954ecb21b8bf68d7f5c52e3c3579cd96f3bde4f76daaaa69a'
281+
'96a5eee268fb8fa2745930c37f0672424136b538878474bc4f586a63e13ae23f'),
265282
)
266283
def test_hash_type_from_composite_str(self, composite: str, e_alg: HashAlgorithm, e_content: str) -> None:
267284
h = HashType.from_composite_str(composite)

0 commit comments

Comments
 (0)