import hashlib import io import logging # Size of the buffer to read files with BUF_SIZE = 4096 LOG = logging.getLogger("acm.utility") def get_file_sha256sum(input_file): sha256sum = hashlib.sha256() with open(input_file, 'rb') as f: for byte_block in iter(lambda: f.read(BUF_SIZE), b""): sha256sum.update(byte_block) return sha256sum.hexdigest() def get_string_sha256sum(content: str, encoding='utf-8') -> str: sha256sum = hashlib.sha256() with io.BytesIO(content.encode(encoding)) as c: for byte_block in iter(lambda: c.read(BUF_SIZE), b''): sha256sum.update(byte_block) return sha256sum.hexdigest() def get_string_hex(content: str) -> hex: return hex(int(content, base=16)) def get_hex_xor(first: hex, second: hex) -> hex: return hex(int(first, base=16) ^ int(second, base=16)) def get_string_xor(first: str, second: str, *extra: str) -> str: result = get_hex_xor(get_string_hex(first), get_string_hex(second)) for next_hex in extra: result = get_hex_xor(result, get_string_hex(next_hex)) return str(result)