Forked from lkdocs/Python PyCrypto: Sign Data Example.py
Created
September 17, 2017 22:39
-
-
Save omgbbqhaxx/d99180141fef81c1a35e0d1e9ea06e20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def sign_data(private_key_loc, data): | |
| ''' | |
| param: private_key_loc Path to your private key | |
| param: package Data to be signed | |
| return: base64 encoded signature | |
| ''' | |
| from Crypto.PublicKey import RSA | |
| from Crypto.Signature import PKCS1_v1_5 | |
| from Crypto.Hash import SHA256 | |
| from base64 import b64encode, b64decode | |
| key = open(private_key_loc, "r").read() | |
| rsakey = RSA.importKey(key) | |
| signer = PKCS1_v1_5.new(rsakey) | |
| digest = SHA256.new() | |
| # It's being assumed the data is base64 encoded, so it's decoded before updating the digest | |
| digest.update(b64decode(data)) | |
| sign = signer.sign(digest) | |
| return b64encode(sign) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment