Last active
November 10, 2023 16:19
-
-
Save APAC-GOLD/b6057cd3049489061115aa69cb646760 to your computer and use it in GitHub Desktop.
Digital Signature
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
#How To - perform a digital signature using RSA256 algorithm in Python | |
##Step 1 - generate an RSA256 key pair (public and private keys) using the command module <Crypto.PublicKey.RSA> to generate the key pair. | |
from Crypto.PublicKey import RSA | |
key = RSA.generate(2048) | |
public_key = key.publickey() | |
print(public_key) #not sure if I need this here but I want to see the output | |
##Step 2 - use the private key to sign an input message. | |
from Crypto.Signature import PKCS1_v1_5 | |
from Crypto.Hash import SHA256 | |
message = b'This is a message to be signed' | |
hash = SHA256.new(message) | |
print(message) #to see the output | |
signer = PKCS1_v1_5.new(key) | |
signature = signer.sign(hash) | |
print(signer) #to see the output | |
print(signature) #to see the output | |
##Step 4 - use the public key to verify the signature | |
verifier = PKCS1_v1_5.new(public_key) | |
if verifier.verify(hash, signature): | |
print("The signature is authentic.") | |
else: | |
print("The signature is not authentic.") | |
#If you need to sign a longer chain of text/ message, use a hash function to generate a fixed-length hash of the message, then sign it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Further Reading. - Cryptography
https://pymotw.com/3/hmac/index.html
https://pymotw.com/3/hashlib/index.html