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)
def decrypt(self, cipher, nonce):
if self.in_k == ('\0' * nacl.crypto_scalarmult_curve25519_BYTES):
# use pk crypto to decrypt the packet
return nacl.crypto_box_open(cipher, nonce, self.peer_id.cp, self.me_id.cs)
else:
# decrypt using chained keys
try:
return nacl.crypto_secretbox_open(cipher, nonce, self.in_k)
except ValueError:
# with previous key in case a prev send failed to be delivered
return nacl.crypto_secretbox_open(cipher, nonce, self.in_prev)
# asym
if self:
me = publickey.Identity(self, basedir=basedir)
if peer:
peer = publickey.Identity(peer, basedir=basedir, publicOnly=True)
sender = None
size = None
i=0
while i < (max_recipients if not size else size):
i+=1
rnonce = fd.read(nacl.crypto_box_NONCEBYTES)
ct = fd.read(nacl.crypto_secretbox_KEYBYTES+2+nacl.crypto_secretbox_MACBYTES)
if sender: continue
for keys in ([peer] if peer else publickey.get_public_keys(basedir=basedir)):
try:
tmp = nacl.crypto_box_open(ct, rnonce, keys.cp, me.cs)
except ValueError:
continue
key = tmp[:nacl.crypto_secretbox_KEYBYTES]
size = struct.unpack('>H',tmp[nacl.crypto_secretbox_KEYBYTES:])[0]
sender = keys.name
break
me.clear()
if not sender:
raise ValueError('decryption failed')
# sym
else:
pwd = getpass.getpass('Passphrase for decrypting: ')
key = scrypt.hash(pwd, scrypt_salt)[:nacl.crypto_secretbox_KEYBYTES]
sender = None
def keydecrypt(self, peers):
for nonce, ck in peers:
for keys in get_public_keys(basedir=self.basedir):
try:
key = nacl.crypto_box_open(ck, nonce, keys.cp, self.cs)
except ValueError:
continue
return (keys.name, key)
return None, None
def DecryptPacket(packet, pk, sk):
nounce = packet[:24]
data = packet[24:]
LAST_NOUNCE = nounce
return pysodium.crypto_box_open(data, nounce, pk, sk)
def load(self):
keyfname="%s/dh/%s/%s" % (self.basedir, self.me, self.id)
if not self.me_id:
self.me_id = publickey.Identity(self.me, basedir=self.basedir)
with open(keyfname,'r') as fd:
nonce = fd.read(nacl.crypto_box_NONCEBYTES)
raw = fd.read()
self.key = nacl.crypto_box_open(raw, nonce, self.me_id.cp, self.me_id.cs)
os.remove(keyfname)
def keydecrypt(self, peers):
for nonce, ck in peers:
for keys in get_public_keys(basedir=self.basedir):
try:
key = nacl.crypto_box_open(ck, nonce, keys.cp, self.cs)
except ValueError:
continue
return (keys.name, key)
return None, None