Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_private_key():
sk = SigningKey.generate(curve=SECP256k1)
with open(PRIV_KEY_LOC, 'wt') as file_obj:
file_obj.write(binascii.b2a_hex(sk.to_string()).decode())
def pubkey_from_signature(sig, h):
if len(sig) != 65:
raise Exception("Wrong encoding")
nV = sig[0]
if nV < 27 or nV >= 35:
raise Exception("Bad encoding")
if nV >= 31:
compressed = True
nV -= 4
else:
compressed = False
recid = nV - 27
return MyVerifyingKey.from_signature(sig[1:], recid, h, curve = SECP256k1), compressed
class MySigningKey(ecdsa.SigningKey):
"""Enforce low S values in signatures"""
def sign_number(self, number, entropy=None, k=None):
curve = SECP256k1
G = curve.generator
order = G.order()
r, s = ecdsa.SigningKey.sign_number(self, number, entropy, k)
if s > order//2:
s = order - s
return r, s
class EC_KEY(object):
def __init__( self, k ):
secret = string_to_number(k)
def sign(cls, message: bytes, seckey: bytes) -> bytes:
"""Signs a message with a secret key.
Args:
message: The binary message to sign.
seckey: The binary representation of the secret key.
Returns:
A binary signature.
"""
sk = ecdsa.SigningKey.from_string(seckey, ecdsa.SECP256k1)
return sk.sign(message)
R = Point(curveFp, x, y, order)
except:
raise InvalidECPointException()
# 1.5 compute e from message:
e = string_to_number(h)
minus_e = -e % order
# 1.6 compute Q = r^-1 (sR - eG)
inv_r = numbertheory.inverse_mod(r,order)
try:
Q = inv_r * ( s * R + minus_e * G )
except:
raise InvalidECPointException()
return klass.from_public_point( Q, curve )
class _MySigningKey(ecdsa.SigningKey):
"""Enforce low S values in signatures"""
def sign_number(self, number, entropy=None, k=None):
r, s = ecdsa.SigningKey.sign_number(self, number, entropy, k)
if s > CURVE_ORDER//2:
s = CURVE_ORDER - s
return r, s
class _PubkeyForPointAtInfinity:
point = ecdsa.ellipticcurve.INFINITY
@functools.total_ordering
class ECPubkey(object):
:param int iv_length: The length in bytes of the IV to return.
:param str digest: The name of hashing function to use to generate the key.
:param str encoding: The name of the encoding to use for the password.
:return: The key and IV as a tuple.
:rtype: tuple
"""
password = password.encode(encoding)
digest_function = getattr(hashlib, digest)
chunk = b''
data = b''
while len(data) < key_length + iv_length:
chunk = digest_function(chunk + password + salt).digest()
data += chunk
return data[:key_length], data[key_length:key_length + iv_length]
class SigningKey(ecdsa.SigningKey, object):
@classmethod
def from_secret_exponent(cls, *args, **kwargs):
instance = super(SigningKey, cls).from_secret_exponent(*args, **kwargs)
orig_vk = instance.verifying_key
instance.verifying_key = VerifyingKey.from_public_point(orig_vk.pubkey.point, instance.curve, instance.default_hashfunc)
return instance
@classmethod
def from_string(cls, string, **kwargs):
kwargs = _kwarg_curve(kwargs)
return super(SigningKey, cls).from_string(string, **kwargs)
@classmethod
def from_dict(cls, value, encoding='base64'):
"""
Load the signing key from the specified dict object.
def create_random_identity():
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
key = sk.to_string()
recipient = sha3_256(sk.verifying_key.to_string())[-20:]
return Identity(recipient, key)