Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ecdh(ecdsa_curve, PrivateKey, PublicKey):
curve = find_curve_oid_hex(ecdsa_curve)
assert not(curve is None)
pub = ecdsa.VerifyingKey.from_string(PublicKey[1:], curve=curve, hashfunc=sha256)
prv = ecdsa.SigningKey.from_string(PrivateKey, curve=curve, hashfunc=sha256)
# shared secret = PUBtheirs * PRIVATEours
result = pub.pubkey.point * prv.privkey.secret_multiplier
shared_secret = ecdsa.util.number_to_string(result.x(), curve.order)
return shared_secret
def _compress_pubkey(self) :
''' '''
order = self._sk.curve.generator.order()
p = self._vk.pubkey.point
x_str = ecdsa.util.number_to_string(p.x(), order)
hex_data = bytearray(chr(2 + (p.y() & 1)), 'utf-8')
compressed = hexlify(hex_data + x_str).decode()
return compressed
def to_string(self):
# VerifyingKey.from_string(vk.to_string()) == vk as long as the
# curves are the same: the curve itself is not included in the
# serialized form
order = self.pubkey.order
x_str = number_to_string(self.pubkey.point.x(), order)
y_str = number_to_string(self.pubkey.point.y(), order)
return x_str + y_str
def compressed_pubkey(self, pk):
order = pk.curve.generator.order()
p = pk.pubkey.point
x_str = ecdsa.util.number_to_string(p.x(), order)
return compat_bytes(chr(2 + (p.y() & 1)), 'ascii') + x_str
from ecdsa.curves import SECP256k1
from ecdsa import VerifyingKey
from ecdsa.util import number_to_string
# Validate his pubkey a little: this call will check it's on the curve.
assert len(his_pubkey) == 64
his_pubkey = VerifyingKey.from_string(his_pubkey, curve=SECP256k1, hashfunc=sha256)
#print("his pubkey = %s" % b2a_hex(his_pubkey.to_string()))
# do the D-H thing
pt = self.my_key.privkey.secret_multiplier * his_pubkey.pubkey.point
# final key is sha256 of that point, serialized (64 bytes).
order = SECP256k1.order
kk = number_to_string(pt.x(), order) + number_to_string(pt.y(), order)
del self.my_key
return sha256(kk).digest()
def compressedPubkey(self, pk):
order = pk.curve.generator.order()
p = pk.pubkey.point
x_str = ecdsa.util.number_to_string(p.x(), order)
return future_bytes(chr(2 + (p.y() & 1)), 'ascii') + x_str
n = self.addresses.index(address)
for_change = False
elif address in self.change_addresses:
n = self.change_addresses.index(address)
for_change = True
else:
raise BaseException("unknown address")
try:
seed = self.pw_decode( self.seed, password)
except:
raise BaseException("Invalid password")
if not seed: return None
secexp = self.stretch_key(seed)
secexp = ( secexp + self.get_sequence(n,for_change) ) % order
pk = number_to_string(secexp,order)
return pk
def from_privkey(cls, privkey, prefix=None):
""" Derive uncompressed public key """
privkey = PrivateKey(privkey, prefix=prefix or Prefix.prefix)
secret = unhexlify(repr(privkey))
order = ecdsa.SigningKey.from_string(
secret, curve=ecdsa.SECP256k1
).curve.generator.order()
p = ecdsa.SigningKey.from_string(
secret, curve=ecdsa.SECP256k1
).verifying_key.pubkey.point
x_str = ecdsa.util.number_to_string(p.x(), order)
# y_str = ecdsa.util.number_to_string(p.y(), order)
compressed = hexlify(chr(2 + (p.y() & 1)).encode("ascii") + x_str).decode(
"ascii"
)
# uncompressed = hexlify(
# chr(4).encode('ascii') + x_str + y_str).decode('ascii')
return cls(compressed, prefix=prefix or Prefix.prefix)
from ecdsa.curves import SECP256k1
from ecdsa import VerifyingKey
from ecdsa.util import number_to_string
# Validate his pubkey a little: this call will check it's on the curve.
assert len(his_pubkey) == 64
his_pubkey = VerifyingKey.from_string(his_pubkey, curve=SECP256k1, hashfunc=sha256)
#print("his pubkey = %s" % b2a_hex(his_pubkey.to_string()))
# do the D-H thing
pt = self.my_key.privkey.secret_multiplier * his_pubkey.pubkey.point
# final key is sha256 of that point, serialized (64 bytes).
order = SECP256k1.order
kk = number_to_string(pt.x(), order) + number_to_string(pt.y(), order)
del self.my_key
return sha256(kk).digest()
def _CKD_priv(k, c, s, is_prime):
order = generator_secp256k1.order()
keypair = EC_KEY(k)
cK = GetPubKey(keypair.pubkey,True)
data = bytes([0]) + k + s if is_prime else cK + s
I = hmac.new(c, data, hashlib.sha512).digest()
k_n = number_to_string( (string_to_number(I[0:32]) + string_to_number(k)) % order , order )
c_n = I[32:]
return k_n, c_n