Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not assume availability of hashing algorithms in hashlib #255

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions org_fedora_oscap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ def get_hashing_algorithm(fingerprint):

"""

hashes = (hashlib.md5(), hashlib.sha1(), hashlib.sha224(),
hashlib.sha256(), hashlib.sha384(), hashlib.sha512())
expected_hash_ids = {'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'}
available_hash_ids = expected_hash_ids.intersection(hashlib.algorithms_available)
hashes = (hashlib.new(hash_id) for hash_id in available_hash_ids)

if len(fingerprint) % 2 == 1:
return None
Expand Down
38 changes: 32 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

from org_fedora_oscap import utils

import hashlib
import warnings


@pytest.fixture()
def mock_os():
Expand Down Expand Up @@ -146,11 +149,34 @@ def test_gen():


def test_hash():
file_hash = '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6'
hash_obj = utils.get_hashing_algorithm(file_hash)
assert hash_obj.name == "sha256"
file_hashes = {
'md5': 'ea38136ca349e139c59f09e09d2aa956',
'sha1': 'f905458483be8ac21002ab2c6409d3a10b3813f1',
'sha224': '2b1e795db6b7397f47a270fbb5059e76b94a8c972240b17c45db1f13',
'sha256': '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6',
'sha384': 'b3ffdfad2bf33caf6e44a8b34386ad741bb80fb02306d3889b8a5645cde31e9d'
'31ec44e0b0e6ce84d83a57339b75b9bf',
'sha512': '7b05940e8d69e804a90f5110d22ad3a1cd03adc5bf4d0a4779790c78118b3c61'
'b7f3a3cd39fcf2902ec92ac80df71b952a7aeb2d53c16f0e77436eeb91e33e1d'
}

for hash_id, file_hash in file_hashes.items():
if hash_id not in hashlib.algorithms_available:
warnings.warn(RuntimeWarning('Expected hash algorithm \'%s\' is not '
'available in this build of Python' % hash_id))
continue

hash_obj = utils.get_hashing_algorithm(file_hash)
assert hash_obj.name == hash_id

filepath = os.path.join(os.path.dirname(__file__), 'data', 'file')
computed_hash = utils.get_file_fingerprint(filepath, hash_obj)
filepath = os.path.join(os.path.dirname(__file__), 'data', 'file')
computed_hash = utils.get_file_fingerprint(filepath, hash_obj)

assert file_hash == computed_hash
assert file_hash == computed_hash


def test_hash_unknown():
file_hash = 'XXXX'

hash_obj = utils.get_hashing_algorithm(file_hash)
assert hash_obj is None
Loading