"""Helpers for cryptography.""" from Crypto.Hash import SHA from Crypto.PublicKey import RSA from Crypto.Signature import pkcs1_15 def verify_signature(public_key_data: bytes, signature: bytes, data: bytes) -> bool: """Verify that the data was signed correctly. Args: public_key_data (bytes): Public key data signature (bytes): Signature to compare against data (bytes): Data which was signed Returns: bool: whether the signature is valid """ key = RSA.import_key(public_key_data) digest = SHA.new(data) # type: ignore signer = pkcs1_15.new(key) try: signer.verify(digest, signature) return True except (ValueError, TypeError): return False