diff --git a/cyclonedx/contrib/hash/factories.py b/cyclonedx/contrib/hash/factories.py index c4b477fd..7cf02562 100644 --- a/cyclonedx/contrib/hash/factories.py +++ b/cyclonedx/contrib/hash/factories.py @@ -87,43 +87,28 @@ def from_composite_str(self, composite_hash: str) -> HashType: Returns: An instance of `HashType`. """ - parts = composite_hash.split(':') + parts = composite_hash.split(':', maxsplit=1) + if len(parts) != 2: + raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') algorithm_prefix = parts[0].lower() + alg = 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() - ) - raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') + alg = HashAlgorithm.BLAKE3 + + if alg is None: + raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') + return HashType(alg=alg, content=parts[1].lower()) diff --git a/tests/test_contrib/test_hash_factories.py b/tests/test_contrib/test_hash_factories.py new file mode 100644 index 00000000..0ea95e5f --- /dev/null +++ b/tests/test_contrib/test_hash_factories.py @@ -0,0 +1,40 @@ +# 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 cyclonedx.contrib.hash.factories import HashTypeFactory +from cyclonedx.exception.model import UnknownHashTypeException + + +class TestHashTypeFactoryFromCompositeStr(TestCase): + + def test_unsupported_blake2_prefix_raises_unknown_hash_type_exception(self) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str( + 'blake2b_256:b7ce149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e' + ) + + def test_unsupported_sha_prefix_raises_unknown_hash_type_exception(self) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str( + 'sha224:724673dee32fdfee47614cc790aefe049b461959e37cb8469c6937ff' + ) + + def test_missing_colon_raises_unknown_hash_type_exception(self) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str('md5')