Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions cyclonedx/contrib/hash/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

__all__ = ['HashTypeFactory']

from typing import Optional

from ...exception.model import UnknownHashTypeException
from ...model import HashAlgorithm, HashType

Expand Down Expand Up @@ -88,42 +90,27 @@ def from_composite_str(self, composite_hash: str) -> HashType:
An instance of `HashType`.
"""
parts = composite_hash.split(':')
if len(parts) < 2:
raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}')

algorithm_prefix = parts[0].lower()
alg: Optional[HashAlgorithm] = None
if algorithm_prefix == 'md5':
return HashType(
alg=HashAlgorithm.MD5,
content=parts[1].lower()
)
alg = HashAlgorithm.MD5
elif algorithm_prefix[0:4] == 'sha3':
return HashType(
alg=getattr(HashAlgorithm, f'SHA3_{algorithm_prefix[5:]}'),
content=parts[1].lower()
)
alg = getattr(HashAlgorithm, f'SHA3_{algorithm_prefix[5:]}', None)
elif algorithm_prefix == 'sha1':
return HashType(
alg=HashAlgorithm.SHA_1,
content=parts[1].lower()
)
alg = HashAlgorithm.SHA_1
elif algorithm_prefix[0:3] == 'sha':
# This is actually SHA2...
return HashType(
alg=getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}'),
content=parts[1].lower()
)
alg = getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}', None)
elif algorithm_prefix[0:7] == 'blake2b':
return HashType(
alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[7:]}'),
content=parts[1].lower()
)
alg = getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[7:]}', None)
elif algorithm_prefix[0:6] == 'blake2':
return HashType(
alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[6:]}'),
content=parts[1].lower()
)
alg = getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[6:]}', None)
elif algorithm_prefix[0:6] == 'blake3':
return HashType(
alg=HashAlgorithm.BLAKE3,
content=parts[1].lower()
)
alg = HashAlgorithm.BLAKE3

if isinstance(alg, HashAlgorithm):
return HashType(alg=alg, content=parts[1].lower())
raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}')
11 changes: 9 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,16 @@ def test_hash_type_from_composite_str(self, composite: str, e_alg: HashAlgorithm
self.assertIs(h.alg, e_alg)
self.assertEqual(h.content, e_content)

def test_hash_type_from_composite_str_throws_on_unknown(self) -> None:
@named_data(
('unknown', 'unknown:dc26cd71b80d6757139f38156a43c545'),
('blake2b_256', 'blake2b_256:b7ce149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e'),
('sha224', 'sha224:724673dee32fdfee47614cc790aefe049b461959e37cb8469c6937ff'),
('sha3-224', 'sha3-224:724673dee32fdfee47614cc790aefe049b461959e37cb8469c6937ff'),
('missing_colon', 'md5'),
)
def test_hash_type_from_composite_str_throws_on_unknown(self, composite: str) -> None:
with self.assertRaises(UnknownHashTypeException):
HashType.from_composite_str('unknown:dc26cd71b80d6757139f38156a43c545')
HashType.from_composite_str(composite)

def test_sort(self) -> None:
# expected sort order: (alg, content)
Expand Down