def DeriveECDHSecret(privateKey, publicKey): """Generate a shared secret from keys in byte format.""" derive = ECDH(curve=NIST256p) derive.load_private_key_bytes(unhexlify(privateKey)) derive.load_received_public_key_bytes(unhexlify(publicKey)) secret = derive.generate_sharedsecret_bytes() return secret
print(f"ECDSA Public key:\n\t" + x.decode("utf-8") + "." + y.decode("utf-8") + "\n\t" + data['pub']) # 3. Check Verification method x, y = data["pub"].split('.') vpub = base64.b64decode(base64urldecode(x)) + base64.b64decode( base64urldecode(y)) vpub = VerifyingKey.from_string(vpub, curve=NIST256p, hashfunc=sha256) integrity = sha256(signed['m'].encode('utf-8')) print( f'Verify signature: {vpub.verify(base64.b64decode(signed["s"]), integrity.digest(), hashfunc=sha256)}' ) # ECDH epriv = base64urldecode(data["epriv"]) epriv = base64.b64decode(epriv) # 1. Decode k ecdh = ECDH(curve=NIST256p) k = ecdh.load_private_key_bytes(epriv) # 2. Generate public key epub = k.to_string() ex, ey = epub[:32], epub[32:] ex = base64urlencode(base64.b64encode(ex)) ey = base64urlencode(base64.b64encode(ey)) print(f"ECDSA Public key:\n\t" + ex.decode("utf-8") + "." + ey.decode("utf-8") + "\n\t" + data['epub'])