spdx_tools.spdx.writer.tagvalue.checksum_writer

 1# SPDX-License-Identifier: Apache-2.0
 2#  Copyright (c) 2022 spdx contributors
 3#  Licensed under the Apache License, Version 2.0 (the "License");
 4#  you may not use this file except in compliance with the License.
 5#  You may obtain a copy of the License at
 6#    http://www.apache.org/licenses/LICENSE-2.0
 7#  Unless required by applicable law or agreed to in writing, software
 8#  distributed under the License is distributed on an "AS IS" BASIS,
 9#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10#  See the License for the specific language governing permissions and
11#  limitations under the License.
12from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm
13
14
15def write_checksum_to_tag_value(checksum: Checksum) -> str:
16    algorithm_name: str = checksum.algorithm.name
17    # Convert underscores to dashes, and other Blake2b-specific casing rules
18    if "_" in algorithm_name:
19        algorithm_name = CHECKSUM_ALGORITHM_TO_TV.get(algorithm_name)
20        if algorithm_name is None:
21            raise ValueError(f"Missing conversion rule for converting {checksum.algorithm.name} to tag-value string")
22    return f"{algorithm_name}: {checksum.value}"
23
24
25CHECKSUM_ALGORITHM_TO_TV = {
26    ChecksumAlgorithm.BLAKE2B_256.name: "BLAKE2b-256",
27    ChecksumAlgorithm.BLAKE2B_384.name: "BLAKE2b-384",
28    ChecksumAlgorithm.BLAKE2B_512.name: "BLAKE2b-512",
29    ChecksumAlgorithm.SHA3_256.name: "SHA3-256",
30    ChecksumAlgorithm.SHA3_384.name: "SHA3-384",
31    ChecksumAlgorithm.SHA3_512.name: "SHA3-512",
32}
def write_checksum_to_tag_value(checksum: spdx_tools.spdx.model.checksum.Checksum) -> str:
16def write_checksum_to_tag_value(checksum: Checksum) -> str:
17    algorithm_name: str = checksum.algorithm.name
18    # Convert underscores to dashes, and other Blake2b-specific casing rules
19    if "_" in algorithm_name:
20        algorithm_name = CHECKSUM_ALGORITHM_TO_TV.get(algorithm_name)
21        if algorithm_name is None:
22            raise ValueError(f"Missing conversion rule for converting {checksum.algorithm.name} to tag-value string")
23    return f"{algorithm_name}: {checksum.value}"
CHECKSUM_ALGORITHM_TO_TV = {'BLAKE2B_256': 'BLAKE2b-256', 'BLAKE2B_384': 'BLAKE2b-384', 'BLAKE2B_512': 'BLAKE2b-512', 'SHA3_256': 'SHA3-256', 'SHA3_384': 'SHA3-384', 'SHA3_512': 'SHA3-512'}