Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use rsa sha256 public key to verify signed text in Python
# packages required but not limited to:
# asn1crypto==0.24.0
# cryptography==2.2.2
# pyOpenSSL==17.5.0
import crypto
# Use the public key file: 'pub_key_filename' to verify
# if 'signed' is a valid one from 'plain_text'
# if it's valid return True, otherwise return False
def verify_signed_text(pub_key_filename, plain_text, signed):
rsa_public_key = None
with open(pub_key_filename, 'r') as pub_key_file:
rsa_public_key =
pkey = crypto.load_publickey(crypto.FILETYPE_PEM, rsa_public_key)
x509 = crypto.X509()
signed = base64.b64decode(signed) # Skip this if signed text is not encoded
crypto.verify(x509, signed,
plain_text.encode('utf-8'), 'sha256')
return True
except crypto.Error as err:
return False
except BaseException as exp:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment