Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_boxnm(self):
msg = b'Are you suggesting coconuts migrate?'
# run 1
nonce1 = libnacl.utils.rand_nonce()
pk1, sk1 = libnacl.crypto_box_keypair()
pk2, sk2 = libnacl.crypto_box_keypair()
k1 = libnacl.crypto_box_beforenm(pk2, sk1)
k2 = libnacl.crypto_box_beforenm(pk1, sk2)
enc_msg = libnacl.crypto_box_afternm(msg, nonce1, k1)
self.assertNotEqual(msg, enc_msg)
clear_msg = libnacl.crypto_box_open_afternm(enc_msg, nonce1, k2)
self.assertEqual(clear_msg, msg)
def test_calc_shared_key_via_two_key_pairs(self):
bob = libnacl.public.SecretKey()
alice = libnacl.public.SecretKey()
alice_key = libnacl.crypto_box_beforenm(bob.pk, alice.sk)
bob_key = libnacl.crypto_box_beforenm(alice.pk, bob.sk)
#print(base58.b58encode(bob_key))
self.assertEqual(bob_key, alice_key)
def test_boxnm(self):
msg = b'Are you suggesting coconuts migrate?'
# run 1
nonce1 = libnacl.utils.rand_nonce()
pk1, sk1 = libnacl.crypto_box_keypair()
pk2, sk2 = libnacl.crypto_box_keypair()
k1 = libnacl.crypto_box_beforenm(pk2, sk1)
k2 = libnacl.crypto_box_beforenm(pk1, sk2)
enc_msg = libnacl.crypto_box_afternm(msg, nonce1, k1)
self.assertNotEqual(msg, enc_msg)
clear_msg = libnacl.crypto_box_open_afternm(enc_msg, nonce1, k2)
self.assertEqual(clear_msg, msg)
def __init__(self, private_key, public_key):
if private_key and public_key:
self._shared_key = libnacl.crypto_box_beforenm(
public_key.encode(encoder=encoding.RawEncoder),
private_key.encode(encoder=encoding.RawEncoder),
)
else:
self._shared_key = None
def calc_session_key(pk, sk):
"""
Calculate the session key from a public key and secret key.
:param pk: the counterpart's public key.
:param sk: the own private key
:return:
"""
return libnacl.crypto_box_beforenm(pk, sk)
def __init__(self, private_key, public_key):
if private_key and public_key:
self._shared_key = libnacl.crypto_box_beforenm(
public_key.encode(encoder=encoding.RawEncoder),
private_key.encode(encoder=encoding.RawEncoder),
)
else:
self._shared_key = None
def generate_diffie_shared_secret(self, dh_received, key=None):
if key is None:
key = self.key
tmp_key = self.generate_key("curve25519")
y = tmp_key.key.sk
Y = tmp_key.key.pk
shared_secret = libnacl.crypto_box_beforenm(dh_received, y) + libnacl.crypto_box_beforenm(dh_received, key.key.sk)
AUTH = libnacl.crypto_auth(Y, shared_secret[:32])
return shared_secret, Y, AUTH
def verify_and_generate_shared_secret(self, dh_secret, dh_received, auth, B):
shared_secret = libnacl.crypto_box_beforenm(dh_received, dh_secret.key.sk) + libnacl.crypto_box_beforenm(B, dh_secret.key.sk)
libnacl.crypto_auth_verify(auth, dh_received, shared_secret[:32])
return shared_secret