diff --git a/cyclonedx/contrib/hash/factories.py b/cyclonedx/contrib/hash/factories.py index c4b477fd..4bf965c5 100644 --- a/cyclonedx/contrib/hash/factories.py +++ b/cyclonedx/contrib/hash/factories.py @@ -41,6 +41,13 @@ } +def _hash_algorithm(name: str) -> HashAlgorithm: + try: + return HashAlgorithm[name] + except KeyError: + raise UnknownHashTypeException(f'Unable to determine hash alg for {name!r}') from None + + class HashTypeFactory: def from_hashlib_alg(self, hashlib_alg: str, content: str) -> HashType: @@ -88,6 +95,8 @@ 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() if algorithm_prefix == 'md5': @@ -97,7 +106,7 @@ def from_composite_str(self, composite_hash: str) -> HashType: ) elif algorithm_prefix[0:4] == 'sha3': return HashType( - alg=getattr(HashAlgorithm, f'SHA3_{algorithm_prefix[5:]}'), + alg=_hash_algorithm(f'SHA3_{algorithm_prefix[5:]}'), content=parts[1].lower() ) elif algorithm_prefix == 'sha1': @@ -108,17 +117,17 @@ def from_composite_str(self, composite_hash: str) -> HashType: elif algorithm_prefix[0:3] == 'sha': # This is actually SHA2... return HashType( - alg=getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}'), + alg=_hash_algorithm(f'SHA_{algorithm_prefix[3:]}'), content=parts[1].lower() ) elif algorithm_prefix[0:7] == 'blake2b': return HashType( - alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[7:]}'), + alg=_hash_algorithm(f'BLAKE2B_{algorithm_prefix[7:]}'), content=parts[1].lower() ) elif algorithm_prefix[0:6] == 'blake2': return HashType( - alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[6:]}'), + alg=_hash_algorithm(f'BLAKE2B_{algorithm_prefix[6:]}'), content=parts[1].lower() ) elif algorithm_prefix[0:6] == 'blake3': diff --git a/tests/test_contrib/test_hash_factories.py b/tests/test_contrib/test_hash_factories.py new file mode 100644 index 00000000..89a3d940 --- /dev/null +++ b/tests/test_contrib/test_hash_factories.py @@ -0,0 +1,65 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) OWASP Foundation. All Rights Reserved. + +from unittest import TestCase + +from ddt import ddt, named_data, unpack + +from cyclonedx.contrib.hash.factories import HashTypeFactory +from cyclonedx.exception.model import UnknownHashTypeException +from cyclonedx.model import HashAlgorithm + + +@ddt +class TestHashTypeFactoryCompositeStr(TestCase): + + @named_data( + ('md5', 'md5:ABC', HashAlgorithm.MD5), + ('sha1', 'sha1:ABC', HashAlgorithm.SHA_1), + ('sha256', 'sha256:ABC', HashAlgorithm.SHA_256), + ('sha3-256', 'sha3-256:ABC', HashAlgorithm.SHA3_256), + ('blake2b256', 'blake2b256:ABC', HashAlgorithm.BLAKE2B_256), + ('blake3', 'blake3:ABC', HashAlgorithm.BLAKE3), + ) + @unpack + def test_known_prefixes(self, composite: str, alg: HashAlgorithm) -> None: + hash_ = HashTypeFactory().from_composite_str(composite) + self.assertIs(alg, hash_.alg) + self.assertEqual('abc', hash_.content) + + @named_data( + ('unknown', 'unknown:abc'), + ('blake2b_256', 'blake2b_256:abc'), + ('sha224', 'sha224:abc'), + ('sha3_224', 'sha3_224:abc'), + ) + @unpack + def test_unsupported_algorithm_raises_unknown_hash_type(self, composite: str) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str(composite) + + @named_data( + ('md5', 'md5'), + ('sha256', 'sha256'), + ('sha3-256', 'sha3-256'), + ('blake3', 'blake3'), + ('empty', ''), + ) + @unpack + def test_missing_separator_raises_unknown_hash_type(self, composite: str) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str(composite)