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_crypto_core_ristretto255_scalar_random(self):
if not pysodium.sodium_version_check(1, 0, 18): return
a = pysodium.crypto_core_ristretto255_scalar_random()
b = pysodium.crypto_core_ristretto255_scalar_random()
# stupid check that random returns different values...
self.assertNotEqual(a,b)
def test_crypto_secretstream_xchacha20poly1305_init_push(self):
if not pysodium.sodium_version_check(1, 0, 15): return
key = pysodium.crypto_secretstream_xchacha20poly1305_keygen()
state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key)
def test_aead_chacha20poly1305_ietf(self):
if not pysodium.sodium_version_check(1, 0, 4): return
key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
nonce = binascii.unhexlify(b"cd7cf67be39c794acd7cf67b")
for ad in [binascii.unhexlify(b"87e229d4500845a079c0"), None]:
output = pysodium.crypto_aead_chacha20poly1305_ietf_encrypt(input_, ad, nonce, key)
output = pysodium.crypto_aead_chacha20poly1305_ietf_decrypt(output, ad, nonce, key)
self.assertEqual(output, input_)
def test_crypto_secretstream_xchacha20poly1305_rekey(self):
if not pysodium.sodium_version_check(1, 0, 15): return
key = pysodium.crypto_secretstream_xchacha20poly1305_keygen()
state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key)
# Encrypt two messages with intermediate re-key
ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0)
pysodium.crypto_secretstream_xchacha20poly1305_rekey(state)
ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
# Verify by decrypting them
state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key)
msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None)
pysodium.crypto_secretstream_xchacha20poly1305_rekey(state2)
msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext2, None)
self.assertEqual(msg, b"Correct Horse Battery Staple")
def test_aead_chacha20poly1305_detached(self):
if not pysodium.sodium_version_check(1, 0, 9): return
key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
nonce = binascii.unhexlify(b"cd7cf67be39c794a")
for ad, ct in [
(binascii.unhexlify(b"87e229d4500845a079c0"), b"677dabf4e3d24b876bb284753896e1d6"),
(None, b"69e7789bcd954e658ed38423e23161dc"),
]:
output, mac = pysodium.crypto_aead_chacha20poly1305_encrypt_detached(input_, ad, nonce, key)
self.assertEqual(binascii.unhexlify(b"e3e446f7ede9a19b62a4"), output)
self.assertEqual(binascii.unhexlify(ct), mac)
output = pysodium.crypto_aead_chacha20poly1305_decrypt_detached(output, mac, ad, nonce, key)
self.assertEqual(output, input_)
def test_crypto_pwhash(self):
if not pysodium.sodium_version_check(1, 0, 9): return
pw = "Correct Horse Battery Staple"
salt = binascii.unhexlify(b'0f58b94c7a369fd8a9a7083e4cd75266')
out = pysodium.crypto_pwhash(pysodium.crypto_auth_KEYBYTES, pw, salt, pysodium.crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE, pysodium.crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE, pysodium.crypto_pwhash_ALG_ARGON2I13)
self.assertEqual(binascii.hexlify(out), b'79db3095517c7358449d84ee3b2f81f0e9907fbd4e0bae4e0bcc6c79821427dc')
def test_crypto_scalarmult_ristretto255_base(self):
if not pysodium.sodium_version_check(1, 0, 18): return
p = pysodium.crypto_scalarmult_ristretto255_base(pysodium.crypto_core_ristretto255_scalar_random())
pysodium.crypto_core_ristretto255_is_valid_point(p)
def test_crypto_secretstream_xchacha20poly1305_pull_corrupted(self):
if not pysodium.sodium_version_check(1, 0, 15): return
key = pysodium.crypto_secretstream_xchacha20poly1305_keygen()
state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key)
ad = 'additional data'
ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", ad, 0)
# Verify error is raised if cypher text is changed
state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key)
self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext + 'this is a corruption'.encode(), ad)
# Verify error is raised if additional data is changed
ad2 = 'this is not the same'
state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key)
self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext, ad2)
def test_crypto_pwhash_storage(self):
if not pysodium.sodium_version_check(1, 0, 9): return
pw = "Correct Horse Battery Staple"
pstr = pysodium.crypto_pwhash_str(pw, pysodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, pysodium.crypto_pwhash_MEMLIMIT_INTERACTIVE)
self.assertTrue(pysodium.crypto_pwhash_str_verify(pstr, pw))
def test_crypto_secretstream_xchacha20poly1305_pull_incorrect_key(self):
if not pysodium.sodium_version_check(1, 0, 15): return
key = pysodium.crypto_secretstream_xchacha20poly1305_keygen()
state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key)
ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
bad_key = pysodium.crypto_secretstream_xchacha20poly1305_keygen()
state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, bad_key)
self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext, None)