Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def convert_der_sig(signature, as_hex=True):
"""
Convert DER encoded signature to signature string
:param signature: DER signature
:type signature: bytes
:param as_hex: Output as hexstring
:type as_hex: bool
:return bytes, str: Signature
"""
if not signature:
return ""
if USE_FASTECDSA:
r, s = DEREncoder.decode_signature(bytes(signature))
else:
sg, junk = ecdsa.der.remove_sequence(signature)
if junk != b'':
raise EncodingError("Junk found in encoding sequence %s" % junk)
r, sg = ecdsa.der.remove_integer(sg)
s, sg = ecdsa.der.remove_integer(sg)
sig = '%064x%064x' % (r, s)
if as_hex:
return sig
else:
return binascii.unhexlify(sig)