11

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?

4
  • 1
    import os; os.system(cmd). Commented Sep 10, 2015 at 15:16
  • @BarunSharma I know it and thank you, but question in another how would I do it with a python functions, libraries? Commented Sep 11, 2015 at 9:17
  • Did you try some luck with opensslpython library. Commented Sep 11, 2015 at 11:20
  • @BarunSharma I found answer:) Thank you for support! Commented Sep 11, 2015 at 15:38

1 Answer 1

25

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
Sign up to request clarification or add additional context in comments.

1 Comment

to exactly match the openssl command in the question what you want to do is binary write the sign to signature.sig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.