From 265e025f6b20ba1b3cf9c96716b9ec02a8694ce0 Mon Sep 17 00:00:00 2001 From: Yan Chen Date: Wed, 2 Dec 2026 08:46:58 +0800 Subject: [PATCH] fix(spdx): support BLAKE2b and Adler-32 in calculate_file_checksum Signed-off-by: Yan Chen --- src/spdx_tools/spdx/spdx_element_utils.py | 26 +++++++++++++++++++++-- tests/spdx/test_checksum_calculation.py | 20 +++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/spdx_tools/spdx/spdx_element_utils.py b/src/spdx_tools/spdx/spdx_element_utils.py index 0d6bd8946..b8dfe1daa 100644 --- a/src/spdx_tools/spdx/spdx_element_utils.py +++ b/src/spdx_tools/spdx/spdx_element_utils.py @@ -2,8 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 import hashlib +import zlib -from beartype.typing import List, Optional, Type, Union +from beartype.typing import Dict, List, Optional, Type, Union from spdx_tools.spdx.model import ( ChecksumAlgorithm, @@ -77,10 +78,31 @@ def calculate_package_verification_code(files: List[File]) -> PackageVerificatio return PackageVerificationCode(value) +_BLAKE2B_DIGEST_SIZES: Dict[ChecksumAlgorithm, int] = { + ChecksumAlgorithm.BLAKE2B_256: 32, + ChecksumAlgorithm.BLAKE2B_384: 48, + ChecksumAlgorithm.BLAKE2B_512: 64, +} + + def calculate_file_checksum(file_name: str, hash_algorithm=ChecksumAlgorithm.SHA1) -> str: BUFFER_SIZE = 65536 - file_hash = hashlib.new(hash_algorithm.name.lower()) + if hash_algorithm == ChecksumAlgorithm.ADLER32: + adler_value = 1 + with open(file_name, "rb") as file_handle: + while True: + data = file_handle.read(BUFFER_SIZE) + if not data: + break + adler_value = zlib.adler32(data, adler_value) + return f"{adler_value & 0xFFFFFFFF:08x}" + + if hash_algorithm in _BLAKE2B_DIGEST_SIZES: + file_hash = hashlib.blake2b(digest_size=_BLAKE2B_DIGEST_SIZES[hash_algorithm]) + else: + file_hash = hashlib.new(hash_algorithm.name.lower()) + with open(file_name, "rb") as file_handle: while True: data = file_handle.read(BUFFER_SIZE) diff --git a/tests/spdx/test_checksum_calculation.py b/tests/spdx/test_checksum_calculation.py index 0e45b11c9..5d929f29e 100644 --- a/tests/spdx/test_checksum_calculation.py +++ b/tests/spdx/test_checksum_calculation.py @@ -1,6 +1,9 @@ # SPDX-FileCopyrightText: 2023 spdx contributors # # SPDX-License-Identifier: Apache-2.0 +import hashlib +import zlib + import pytest from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, File, PackageVerificationCode @@ -28,6 +31,23 @@ def test_file_checksum_calculation(generate_test_files): assert checksum == "140dc52658e2eeee3fdc4d471cce84fec7253fe3" +@pytest.mark.parametrize( + "algorithm, expected_fn", + [ + (ChecksumAlgorithm.BLAKE2B_256, lambda data: hashlib.blake2b(data, digest_size=32).hexdigest()), + (ChecksumAlgorithm.BLAKE2B_384, lambda data: hashlib.blake2b(data, digest_size=48).hexdigest()), + (ChecksumAlgorithm.BLAKE2B_512, lambda data: hashlib.blake2b(data, digest_size=64).hexdigest()), + (ChecksumAlgorithm.ADLER32, lambda data: f"{zlib.adler32(data) & 0xFFFFFFFF:08x}"), + ], +) +def test_file_checksum_calculation_blake2b_and_adler32(tmp_path, algorithm, expected_fn): + payload = b"spdx-checksum-" * 5000 # spans multiple 64 KiB read chunks + file_path = tmp_path.joinpath("multi_chunk.bin") + file_path.write_bytes(payload) + + assert calculate_file_checksum(str(file_path), algorithm) == expected_fn(payload) + + def test_verification_code_calculation_with_predefined_checksums(generate_test_files): filepath1, filepath2 = generate_test_files file1 = File(