Python Forum
[cryptography.io] How to convert DER signature to ECDSA
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[cryptography.io] How to convert DER signature to ECDSA
#1
Hello all,

According to cryptography.io documentation the signature for
cryptography.hazmat.primitives.asymmetric.ec.ECDSA
is a bytes object, whose contents is DER encoded
and can be decoded using decode_dss_signature()

and decode_dss_signature is returning decoded tuple (r,s)

r, s = decode_dss_signature(signature)
How to properly concatenate (r,s) to binary again.

Found something like
r_bytes = r.to_bytes((r.bit_length() + 7) // 8, 'big')
s_bytes = s.to_bytes((s.bit_length() + 7) // 8, 'big')
Is this correct? and if so what's next?
Reply
#2
In the end I went for openssl with subprocess

from codecs import getdecoder
from re import findall
from subprocess import Popen, PIPE

with open('app.dat', 'rb') as dat_file:
    signable_content = dat_file.read()
    out = Popen([
        'openssl',
        'dgst',
        '-sha256',
        '-binary',
        '-sign', 'private-key.pem',
    ], stdout=PIPE, stderr=PIPE, stdin=PIPE)
    signature_output, err = out.communicate(input=signable_content)
    # Extract raw data from ASN.1 DER signature
    out = Popen([
        'openssl',
        'asn1parse',
        '-inform',
        'DER',
    ], stdout=PIPE, stderr=PIPE, stdin=PIPE)
    formatted_signature, err = out.communicate(input=signature_output)
    formatted_signature = formatted_signature.decode('utf-8')
    # Extract only R and S values
    regex = r'\b[0-9A-F]{64}\b'
    rs_values = findall(regex, formatted_signature)
    # Concatenate R and S values
    rs_values = ''.join(str(v) for v in rs_values)
    decode_hex = getdecoder('hex_codec')
    signature = decode_hex(rs_values)[0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Support Python loop ECDSA Secp256k1 MaxiMous 0 1,889 Mar-21-2021, 04:34 PM
Last Post: MaxiMous
  Remove Email Signature NewBeie 4 9,323 Jan-01-2020, 06:44 PM
Last Post: PythonPaul2016
  Cryptography in Python NLittle17 6 4,269 Jan-13-2019, 07:11 PM
Last Post: NLittle17
  Signature verification saisankalpj 19 8,148 Nov-22-2018, 01:55 PM
Last Post: saisankalpj
  Signature verification saisankalpj 8 5,241 Nov-20-2018, 09:32 AM
Last Post: saisankalpj
  cryptography help jorrdn 3 3,125 Mar-10-2018, 02:29 PM
Last Post: sparkz_alot
  Python cryptography or pycrypto lexlukkia 4 53,852 Jan-25-2017, 12:08 PM
Last Post: wavic
  With Python I cannot calculate an AWS signature for Rest APIs Johno 4 6,479 Oct-06-2016, 11:05 AM
Last Post: Johno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020