Skip to content

Instantly share code, notes, and snippets.

@morontt
Created May 30, 2017 08:24
Show Gist options
  • Save morontt/ae6f1edddb07e4fbe2f07a9f1a59d456 to your computer and use it in GitHub Desktop.
Save morontt/ae6f1edddb07e4fbe2f07a9f1a59d456 to your computer and use it in GitHub Desktop.
RSA signature
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from base64 import b64encode
config = ConfigParser.ConfigParser()
config.readfp(open('config.ini'))
# test signature
#
# echo -n 'hello world' | openssl dgst -sha1 -sign private.pem | base64 > signature.txt
# base64 --decode -i signature.txt > signature-decoded.bin
# echo -n 'hello world' | openssl dgst -sha1 -verify public.pem -signature signature-decoded.bin
data = 'hello world'
key_file = config.get('secret', 'private_key_path')
print key_file
key = None
try:
key = open(key_file, 'r').read()
except IOError:
print 'private key not found'
exit(1)
rsa_key = RSA.importKey(key)
signer = PKCS1_v1_5.new(rsa_key)
digest = SHA.new()
digest.update(data)
signature = b64encode(signer.sign(digest))
print signature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment