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_secret_box(self):
msg = b'Are you suggesting coconuts migrate?'
sk1 = libnacl.utils.salsa_key()
nonce1 = libnacl.utils.rand_nonce()
enc_msg = libnacl.crypto_secretbox(msg, nonce1, sk1)
self.assertNotEqual(msg, enc_msg)
clear_msg = libnacl.crypto_secretbox_open(enc_msg, nonce1, sk1)
self.assertEqual(msg, clear_msg)
def test_nonce_generation(self):
prev_nonce = libnacl.utils.rand_nonce()
for i in xrange(100):
nonce = libnacl.utils.rand_nonce()
self.assertNotEqual(prev_nonce, nonce)
prev_nonce = nonce
def test_auth_verify(self):
msg = b'Anybody can invent a cryptosystem he cannot break himself. Except Bruce Schneier.'
key1 = libnacl.utils.salsa_key()
key2 = libnacl.utils.salsa_key()
sig1 = libnacl.crypto_auth(msg, key1)
sig2 = libnacl.crypto_auth(msg, key2)
self.assertTrue(libnacl.crypto_auth_verify(sig1, msg, key1))
self.assertTrue(libnacl.crypto_auth_verify(sig2, msg, key2))
with self.assertRaises(ValueError) as context:
libnacl.crypto_auth_verify(sig1, msg, key2)
self.assertTrue('Failed to auth msg' in context.exception.args)
with self.assertRaises(ValueError) as context:
libnacl.crypto_auth_verify(sig2, msg, key1)
self.assertTrue('Failed to auth msg' in context.exception.args)
def test_auth_verify(self):
msg = b'Anybody can invent a cryptosystem he cannot break himself. Except Bruce Schneier.'
key1 = libnacl.utils.salsa_key()
key2 = libnacl.utils.salsa_key()
sig1 = libnacl.crypto_auth(msg, key1)
sig2 = libnacl.crypto_auth(msg, key2)
self.assertTrue(libnacl.crypto_auth_verify(sig1, msg, key1))
self.assertTrue(libnacl.crypto_auth_verify(sig2, msg, key2))
with self.assertRaises(ValueError) as context:
libnacl.crypto_auth_verify(sig1, msg, key2)
self.assertTrue('Failed to auth msg' in context.exception.args)
with self.assertRaises(ValueError) as context:
libnacl.crypto_auth_verify(sig2, msg, key1)
self.assertTrue('Failed to auth msg' in context.exception.args)
def test_key_blake(self):
msg1 = b'Are you suggesting coconuts migrate?'
msg2 = b'Not at all, they could be carried.'
key1 = libnacl.utils.rand_nonce()
key2 = libnacl.utils.rand_nonce()
khash1_1 = libnacl.blake.Blake2b(msg1, key1).digest()
khash1_1_2 = libnacl.blake.Blake2b(msg1, key1).digest()
khash1_2 = libnacl.blake.Blake2b(msg1, key2).digest()
khash2_1 = libnacl.blake.blake2b(msg2, key1).digest()
khash2_2 = libnacl.blake.blake2b(msg2, key2).digest()
self.assertNotEqual(msg1, khash1_1)
self.assertNotEqual(msg1, khash1_2)
self.assertNotEqual(msg2, khash2_1)
self.assertNotEqual(msg2, khash2_2)
self.assertNotEqual(khash1_1, khash1_2)
self.assertNotEqual(khash2_1, khash2_2)
self.assertNotEqual(khash1_1, khash2_1)
self.assertNotEqual(khash1_2, khash2_2)
self.assertEqual(khash1_1, khash1_1_2)
def test_save_load(self):
msg = b'then leap out of the rabbit, taking the French by surprise'
bob = libnacl.dual.DualSecret()
alice = libnacl.dual.DualSecret()
fh_, bob_path = tempfile.mkstemp()
os.close(fh_)
fh_, alice_path = tempfile.mkstemp()
os.close(fh_)
bob.save(bob_path)
alice.save(alice_path)
bob_box = libnacl.public.Box(bob, alice.pk)
alice_box = libnacl.public.Box(alice, bob.pk)
bob_enc = bob_box.encrypt(msg)
alice_enc = alice_box.encrypt(msg)
bob_load = libnacl.utils.load_key(bob_path)
alice_load = libnacl.utils.load_key(alice_path)
bob_load_box = libnacl.public.Box(bob_load, alice_load.pk)
alice_load_box = libnacl.public.Box(alice_load, bob_load.pk)
self.assertEqual(bob.sk, bob_load.sk)
self.assertEqual(bob.pk, bob_load.pk)
self.assertEqual(bob.vk, bob_load.vk)
self.assertEqual(bob.seed, bob_load.seed)
self.assertEqual(alice.sk, alice_load.sk)
self.assertEqual(alice.pk, alice_load.pk)
self.assertEqual(alice.vk, alice_load.vk)
self.assertEqual(alice.seed, alice_load.seed)
bob_dec = alice_load_box.decrypt(bob_enc)
alice_dec = bob_load_box.decrypt(alice_enc)
self.assertEqual(bob_dec, msg)
self.assertEqual(alice_dec, msg)
os.remove(bob_path)
def test_secret_box(self):
msg = b'Are you suggesting coconuts migrate?'
sk1 = libnacl.utils.salsa_key()
nonce1 = libnacl.utils.rand_nonce()
enc_msg = libnacl.crypto_secretbox(msg, nonce1, sk1)
self.assertNotEqual(msg, enc_msg)
clear_msg = libnacl.crypto_secretbox_open(enc_msg, nonce1, sk1)
self.assertEqual(msg, clear_msg)
def encrypt(self, msg, nonce=None):
'''
Encrypt the given message. If a nonce is not given it will be
generated via the rand_nonce function
'''
if nonce is None:
nonce = libnacl.utils.rand_nonce()
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
raise ValueError('Invalid Nonce')
ctxt = libnacl.crypto_secretbox(msg, nonce, self.sk)
return nonce + ctxt
def encrypt(self, msg, nonce=None, pack_nonce=True):
'''
Encrypt the given message. If a nonce is not given it will be
generated via the rand_nonce function
'''
if nonce is None:
nonce = libnacl.utils.rand_nonce()
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
raise ValueError('Invalid nonce size')
ctxt = libnacl.crypto_secretbox(msg, nonce, self.sk)
if pack_nonce:
return nonce + ctxt
else:
return nonce, ctxt
def __init__(self, key=None):
if key is None:
key = libnacl.utils.salsa_key()
if len(key) != libnacl.crypto_secretbox_KEYBYTES:
raise ValueError('Invalid key')
self.sk = key