spdx_tools.spdx3.model.hash

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from enum import Enum, auto
 5
 6from beartype.typing import Optional
 7
 8from spdx_tools.common.typing.dataclass_with_properties import dataclass_with_properties
 9from spdx_tools.common.typing.type_checks import check_types_and_set_values
10from spdx_tools.spdx3.model import IntegrityMethod
11
12
13class HashAlgorithm(Enum):
14    BLAKE2B256 = auto()
15    BLAKE2B384 = auto()
16    BLAKE2B512 = auto()
17    BLAKE3 = auto()
18    CRYSTALS_KYBER = auto()
19    CRYSTALS_DILITHIUM = auto()
20    FALCON = auto()
21    MD2 = auto()
22    MD4 = auto()
23    MD5 = auto()
24    MD6 = auto()
25    OTHER = auto()
26    SHA1 = auto()
27    SHA224 = auto()
28    SHA256 = auto()
29    SHA3_224 = auto()
30    SHA3_256 = auto()
31    SHA3_384 = auto()
32    SHA3_512 = auto()
33    SHA384 = auto()
34    SHA512 = auto()
35    SPDXPVCSHA1 = auto()
36    SPDXPVCSHA256 = auto()
37    SPHINCS_PLUS = auto()
38
39
40@dataclass_with_properties
41class Hash(IntegrityMethod):
42    algorithm: HashAlgorithm = None
43    hash_value: str = None
44
45    def __init__(self, algorithm: HashAlgorithm, hash_value: str, comment: Optional[str] = None):
46        check_types_and_set_values(self, locals())
class HashAlgorithm(enum.Enum):
14class HashAlgorithm(Enum):
15    BLAKE2B256 = auto()
16    BLAKE2B384 = auto()
17    BLAKE2B512 = auto()
18    BLAKE3 = auto()
19    CRYSTALS_KYBER = auto()
20    CRYSTALS_DILITHIUM = auto()
21    FALCON = auto()
22    MD2 = auto()
23    MD4 = auto()
24    MD5 = auto()
25    MD6 = auto()
26    OTHER = auto()
27    SHA1 = auto()
28    SHA224 = auto()
29    SHA256 = auto()
30    SHA3_224 = auto()
31    SHA3_256 = auto()
32    SHA3_384 = auto()
33    SHA3_512 = auto()
34    SHA384 = auto()
35    SHA512 = auto()
36    SPDXPVCSHA1 = auto()
37    SPDXPVCSHA256 = auto()
38    SPHINCS_PLUS = auto()

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

BLAKE2B256 = <HashAlgorithm.BLAKE2B256: 1>
BLAKE2B384 = <HashAlgorithm.BLAKE2B384: 2>
BLAKE2B512 = <HashAlgorithm.BLAKE2B512: 3>
BLAKE3 = <HashAlgorithm.BLAKE3: 4>
CRYSTALS_KYBER = <HashAlgorithm.CRYSTALS_KYBER: 5>
CRYSTALS_DILITHIUM = <HashAlgorithm.CRYSTALS_DILITHIUM: 6>
FALCON = <HashAlgorithm.FALCON: 7>
MD2 = <HashAlgorithm.MD2: 8>
MD4 = <HashAlgorithm.MD4: 9>
MD5 = <HashAlgorithm.MD5: 10>
MD6 = <HashAlgorithm.MD6: 11>
OTHER = <HashAlgorithm.OTHER: 12>
SHA1 = <HashAlgorithm.SHA1: 13>
SHA224 = <HashAlgorithm.SHA224: 14>
SHA256 = <HashAlgorithm.SHA256: 15>
SHA3_224 = <HashAlgorithm.SHA3_224: 16>
SHA3_256 = <HashAlgorithm.SHA3_256: 17>
SHA3_384 = <HashAlgorithm.SHA3_384: 18>
SHA3_512 = <HashAlgorithm.SHA3_512: 19>
SHA384 = <HashAlgorithm.SHA384: 20>
SHA512 = <HashAlgorithm.SHA512: 21>
SPDXPVCSHA1 = <HashAlgorithm.SPDXPVCSHA1: 22>
SPDXPVCSHA256 = <HashAlgorithm.SPDXPVCSHA256: 23>
SPHINCS_PLUS = <HashAlgorithm.SPHINCS_PLUS: 24>
Inherited Members
enum.Enum
name
value
@dataclass_with_properties
class Hash(spdx_tools.spdx3.model.integrity_method.IntegrityMethod):
41@dataclass_with_properties
42class Hash(IntegrityMethod):
43    algorithm: HashAlgorithm = None
44    hash_value: str = None
45
46    def __init__(self, algorithm: HashAlgorithm, hash_value: str, comment: Optional[str] = None):
47        check_types_and_set_values(self, locals())
Hash( algorithm: HashAlgorithm, hash_value: str, comment: Optional[str] = None)
46    def __init__(self, algorithm: HashAlgorithm, hash_value: str, comment: Optional[str] = None):
47        check_types_and_set_values(self, locals())
algorithm: HashAlgorithm
47    def get_field(self) -> field_type:
48        return getattr(self, f"_{field_name}")
hash_value: str
47    def get_field(self) -> field_type:
48        return getattr(self, f"_{field_name}")