3737import py_serializable as serializable
3838from sortedcontainers import SortedSet
3939
40+ from contrib .hash .factories import HashTypeFactory
4041from .._internal .compare import ComparableTuple as _ComparableTuple
41- from ..exception .model import InvalidLocaleTypeException , InvalidUriException , UnknownHashTypeException
42+ from ..exception .model import InvalidLocaleTypeException , InvalidUriException
4243from ..exception .serialization import CycloneDxDeserializationException , SerializationOfUnexpectedValueException
4344from ..schema .schema import (
4445 SchemaVersion1Dot0 ,
@@ -366,25 +367,6 @@ def xml_denormalize(cls, o: 'XmlElement', *,
366367 ]
367368
368369
369- _MAP_HASHLIB : dict [str , HashAlgorithm ] = {
370- # from hashlib.algorithms_guaranteed
371- 'md5' : HashAlgorithm .MD5 ,
372- 'sha1' : HashAlgorithm .SHA_1 ,
373- # sha224:
374- 'sha256' : HashAlgorithm .SHA_256 ,
375- 'sha384' : HashAlgorithm .SHA_384 ,
376- 'sha512' : HashAlgorithm .SHA_512 ,
377- # blake2b:
378- # blake2s:
379- # sha3_224:
380- 'sha3_256' : HashAlgorithm .SHA3_256 ,
381- 'sha3_384' : HashAlgorithm .SHA3_384 ,
382- 'sha3_512' : HashAlgorithm .SHA3_512 ,
383- # shake_128:
384- # shake_256:
385- }
386-
387-
388370@serializable .serializable_class
389371class HashType :
390372 """
@@ -395,91 +377,27 @@ class HashType:
395377 """
396378
397379 @staticmethod
398- def from_hashlib_alg (hashlib_alg : str , content : str ) -> 'HashType' : # TODO: move to contrib
399- """
400- Attempts to convert a hashlib-algorithm to our internal model classes.
401-
402- Args:
403- hashlib_alg:
404- Hash algorith - like it is used by `hashlib`.
405- Example: `sha256`.
380+ def from_hashlib_alg (hashlib_alg : str , content : str ) -> 'HashType' :
381+ """Deprecated — Alias of :func:`cyclonedx.contrib.hash.factories.HashTypeFactory.from_hashlib_alge`.
406382
407- content:
408- Hash value.
409-
410- Raises:
411- `UnknownHashTypeException` if the algorithm of hash cannot be determined.
383+ Attempts to convert a hashlib-algorithm to our internal model classes.
412384
413- Returns:
414- An instance of `HashType` .
385+ .. deprecated:: next
386+ Use ``cyclonedx.contrib.hash.factories.HashTypeFactory.from_hashlib_alg()`` instead .
415387 """
416- alg = _MAP_HASHLIB .get (hashlib_alg .lower ())
417- if alg is None :
418- raise UnknownHashTypeException (f'Unable to determine hash alg for { hashlib_alg !r} ' )
419- return HashType (alg = alg , content = content )
388+ return HashTypeFactory ().from_hashlib_alg (hashlib_alg , content )
420389
421390 @staticmethod
422- def from_composite_str (composite_hash : str ) -> 'HashType' : # TODO: move to contrib
423- """
391+ def from_composite_str (composite_hash : str ) -> 'HashType' :
392+ """Deprecated — Alias of :func:`cyclonedx.contrib.hash.factories.HashTypeFactory.from_composite_str`.
393+
424394 Attempts to convert a string which includes both the Hash Algorithm and Hash Value and represent using our
425395 internal model classes.
426396
427- Args:
428- composite_hash:
429- Composite Hash string of the format `HASH_ALGORITHM`:`HASH_VALUE`.
430- Example: `sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b`.
431-
432- Valid case insensitive prefixes are:
433- `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `blake2b256`, `blake2b384`, `blake2b512`,
434- `blake2256`, `blake2384`, `blake2512`, `sha3-256`, `sha3-384`, `sha3-512`,
435- `blake3`.
436-
437- Raises:
438- `UnknownHashTypeException` if the type of hash cannot be determined.
439-
440- Returns:
441- An instance of `HashType`.
397+ .. deprecated:: next
398+ Use ``cyclonedx.contrib.hash.factories.HashTypeFactory.from_composite_str()`` instead.
442399 """
443- parts = composite_hash .split (':' )
444-
445- algorithm_prefix = parts [0 ].lower ()
446- if algorithm_prefix == 'md5' :
447- return HashType (
448- alg = HashAlgorithm .MD5 ,
449- content = parts [1 ].lower ()
450- )
451- elif algorithm_prefix [0 :4 ] == 'sha3' :
452- return HashType (
453- alg = getattr (HashAlgorithm , f'SHA3_{ algorithm_prefix [5 :]} ' ),
454- content = parts [1 ].lower ()
455- )
456- elif algorithm_prefix == 'sha1' :
457- return HashType (
458- alg = HashAlgorithm .SHA_1 ,
459- content = parts [1 ].lower ()
460- )
461- elif algorithm_prefix [0 :3 ] == 'sha' :
462- # This is actually SHA2...
463- return HashType (
464- alg = getattr (HashAlgorithm , f'SHA_{ algorithm_prefix [3 :]} ' ),
465- content = parts [1 ].lower ()
466- )
467- elif algorithm_prefix [0 :7 ] == 'blake2b' :
468- return HashType (
469- alg = getattr (HashAlgorithm , f'BLAKE2B_{ algorithm_prefix [7 :]} ' ),
470- content = parts [1 ].lower ()
471- )
472- elif algorithm_prefix [0 :6 ] == 'blake2' :
473- return HashType (
474- alg = getattr (HashAlgorithm , f'BLAKE2B_{ algorithm_prefix [6 :]} ' ),
475- content = parts [1 ].lower ()
476- )
477- elif algorithm_prefix [0 :6 ] == 'blake3' :
478- return HashType (
479- alg = HashAlgorithm .BLAKE3 ,
480- content = parts [1 ].lower ()
481- )
482- raise UnknownHashTypeException (f'Unable to determine hash type from { composite_hash !r} ' )
400+ return HashTypeFactory ().from_composite_str (composite_hash )
483401
484402 def __init__ (
485403 self , * ,
0 commit comments