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_secretbox_open(self):
k = pysodium.randombytes(pysodium.crypto_secretbox_KEYBYTES)
n = pysodium.randombytes(pysodium.crypto_secretbox_NONCEBYTES)
c = pysodium.crypto_secretbox(b"howdy", n, k)
pysodium.crypto_secretbox_open(c, n, k)
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
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
clearmem(pwd)
pwd=None
if key:
nonce = fd.read(nacl.crypto_secretbox_NONCEBYTES)
buf = fd.read(BLOCK_SIZE + nacl.crypto_secretbox_MACBYTES)
while buf:
outfd.write(decrypt((nonce, buf), k = key))
nonce = inc_nonce(nonce)
buf = fd.read(BLOCK_SIZE + nacl.crypto_secretbox_MACBYTES)
clearmem(key)
key = None
if fd != sys.stdin: fd.close()
if outfd != sys.stdout and type(outfd) == file: outfd.close()
return sender
def savesecretekey(self, ext, key):
fname = get_sk_filename(self.basedir, self.name, ext)
k = pbp.getkey(nacl.crypto_secretbox_KEYBYTES,
empty=True,
text='Master' if ext == 'mk' else 'Subkey')
nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES)
with open(fname,'w') as fd:
fd.write(nonce)
fd.write(nacl.crypto_secretbox(key, nonce, k))
def encrypt(msg, pwd=None, k=None, nonce=None):
# encrypts a message symmetrically using crypto_secretbox
# k specifies an encryption key, which if not supplied, is derived from
# pwd which is queried from the user, if also not specified.
# returns a (nonce, ciphertext) tuple
if nonce==None:
nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES)
clearpwd = (pwd is None)
cleark = (k is None)
if not k:
k = getkey(nacl.crypto_secretbox_KEYBYTES, pwd=pwd)
ciphertext = nacl.crypto_secretbox(msg, nonce, k)
if cleark and k:
clearmem(k)
k = None
if clearpwd and pwd:
clearmem(pwd)
pwd = None
return (nonce, ciphertext)
def decrypt_with_user_pw(self, filename, pw_for):
with file(filename) as fd:
nonce = fd.read(nacl.crypto_secretbox_NONCEBYTES)
prompt = 'Passphrase for decrypting {0} for {1}: '.format(pw_for, self.name)
k = scrypt.hash(getpass.getpass(prompt), pbp.scrypt_salt)[:nacl.crypto_secretbox_KEYBYTES]
return nacl.crypto_secretbox_open(fd.read(), nonce, k)
def decrypt_with_user_pw(self, filename, pw_for):
with file(filename) as fd:
nonce = fd.read(nacl.crypto_secretbox_NONCEBYTES)
prompt = 'Passphrase for decrypting {0} for {1}: '.format(pw_for, self.name)
k = scrypt.hash(getpass.getpass(prompt), pbp.scrypt_salt)[:nacl.crypto_secretbox_KEYBYTES]
return nacl.crypto_secretbox_open(fd.read(), nonce, k)
def encrypt(self, msg, recipients=None):
mk = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES)
c = self.keyencrypt(mk, recipients)
nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES)
return (nonce, c, nacl.crypto_secretbox(msg, nonce, mk))