from tempfile import SpooledTemporaryFile import cryptography.hazmat.primitives.hashes def get_hash(data): if isinstance(data, str): data = data.encode('utf-8') digest = cryptography.hazmat.primitives.hashes.Hash(cryptography.hazmat.primitives.hashes.SHA256()) digest.update(data) return digest.finalize().hex() def get_hex_from_temp_file(temp_file: SpooledTemporaryFile) -> bytes: temp_file.seek(0) file_data = temp_file.read() return file_data