How to use the pysodium.crypto_secretbox function in pysodium

To help you get started, we’ve selected a few pysodium examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stef / pbp / pbp / publickey.py View on Github external
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))
github stef / pbp / publickey.py View on Github external
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))
github stef / pbp / pbp / publickey.py View on Github external
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))
github stef / pbp / pbp / chaining.py View on Github external
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
github stef / pbp / pbp / pbp.py View on Github external
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)
github stef / pbp / publickey.py View on Github external
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))