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_box_open(self):
m = b"howdy"
pk, sk = pysodium.crypto_box_keypair()
n = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES)
c = pysodium.crypto_box(m, n, pk, sk)
plaintext = pysodium.crypto_box_open(c, n, pk, sk)
self.assertEqual(m, plaintext)
keyfdir="%s/sk/.%s" % (self.basedir, self.me)
if not os.path.exists(keyfdir):
os.mkdir(keyfdir)
fname='%s/%s' % (keyfdir, self.peer)
nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
ctx=''.join((self.e_in,
self.e_out,
self.peer_pub,
self.out_k,
self.in_k,
self.in_prev))
if not self.me_id:
self.me_id = publickey.Identity(self.me, basedir=self.basedir)
with open(fname,'w') as fd:
fd.write(nonce)
fd.write(nacl.crypto_box(ctx, nonce, self.me_id.cp, self.me_id.cs))
def keyencrypt(self, key, recipients=None):
c=[]
for r in recipients:
nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
c.append((nonce, nacl.crypto_box(key, nonce, r.cp, self.cs)))
return c
def encrypt(self,plain):
if self.out_k == ('\0' * nacl.crypto_scalarmult_curve25519_BYTES):
# encrypt using public key
nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
cipher= nacl.crypto_box(plain, nonce, self.peer_id.cp, self.me_id.cs)
else:
# encrypt using chaining mode
nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES)
cipher = nacl.crypto_secretbox(plain, nonce, self.out_k)
return cipher, nonce
def keyencrypt(self, key, recipients=None):
c=[]
for r in recipients:
nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
c.append((nonce, nacl.crypto_box(key, nonce, r.cp, self.cs)))
return c
def EncryptPacket(packet, pk, sk):
nounce = LAST_NOUNCE
data = pysodium.crypto_box(packet, nounce, pk, sk)
return struct.pack("