import hashlib import argparse import sys try: import libarchive except ImportError: sys.stderr.write('The libarchive package is missing. Please install libarchive-c first.\n\nsudo apt install python3-libarchive-c\n\n') sys.exit(1) parser = argparse.ArgumentParser() parser.add_argument('infile', nargs="+") parser.add_argument('-c', '--hashtype', default="md5", choices=hashlib.algorithms_available) args = parser.parse_args() def hash_files_in_archive(archive_path): try: with libarchive.file_reader(archive_path) as archive: for entry in archive: hasher = hashlib.new(args.hashtype) for block in entry.get_blocks(): hasher.update(block) print(f'{hasher.hexdigest()} {entry.pathname}') except AttributeError: sys.stderr.write("It seems you're using a version of libarchive that doesn't have the 'file_reader' attribute. " "This script requires 'python-libarchive-c'. Please ensure it's installed.\n") sys.exit(2) # Example usage: for infile in args.infile: try: hash_files_in_archive(infile) except libarchive.exception.ArchiveError as e: sys.stderr.write(f'Failed to open archive file {infile}: {e}\n')