Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active October 1, 2020 06:11
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lkdocs/6519366 to your computer and use it in GitHub Desktop.
Save lkdocs/6519366 to your computer and use it in GitHub Desktop.
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