I am currently signing data using next opensll command:
openssl dgst -sign key.pem -sha256 -out signature.sig data.txt
How can I achieve this with Python?
I found answer here(http://nullege.com/ and https://pyopenssl.readthedocs.org/en/stable/api/crypto.html):
import OpenSSL
from OpenSSL import crypto
import base64
key_file = open("C:\my.pem", "r")
key = key_file.read()
key_file.close()
password = "password of prk"
if key.startswith('-----BEGIN '):
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key, password)
else:
pkey = crypto.load_pkcs12(key, password).get_privatekey()
print pkey
data = "data"
sign = OpenSSL.crypto.sign(pkey, data, "sha256")
print sign
data_base64 = base64.b64encode(sign)
print data_base64
openssl command in the question what you want to do is binary write the sign to signature.sig
import os; os.system(cmd).opensslpython library.