Created
June 10, 2011 04:27
-
-
Save mjallday/1018228 to your computer and use it in GitHub Desktop.
Example of digital signature signing and verification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from M2Crypto import EVP, RSA, X509 | |
import binascii | |
class C(object): | |
def __init__(self): | |
pass | |
def sign(self, message_to_encrypt): | |
key = EVP.load_key("mycert-private.pem") | |
key.reset_context(md='sha256') | |
key.sign_init() | |
key.sign_update(message_to_encrypt) | |
signature = key.sign_final() | |
hex_signature = binascii.b2a_hex(signature) | |
return hex_signature | |
def verify(self, message, encrypted_message): | |
java_sig = binascii.a2b_hex(encrypted_message) | |
cert = X509.load_cert("mycert.pem") | |
pubkey = cert.get_pubkey() | |
pubkey.reset_context(md="sha256") | |
pubkey.verify_init() | |
pubkey.verify_update(message) | |
assert pubkey.verify_final(java_sig) == 1 | |
message = "something" | |
c = C() | |
encrypted_message = c.sign(message) | |
c.verify(message, encrypted_message) | |
message = " something esle" | |
try: | |
c.verify(message, encrypted_message) | |
except AssertionError: | |
has_error = True | |
else: | |
has_error = False | |
assert has_error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment