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?
Currently Python versions 3.6 to 3.9 are compatible with OpenSSL 1.0.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With