How to use the pysodium.crypto_box_NONCEBYTES 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 / pysodium / test / test_pysodium.py View on Github external
def test_crypto_box_open_afternm(self):
        m = b"howdy"
        pk, sk = pysodium.crypto_box_keypair()
        k = pysodium.crypto_box_beforenm(pk, sk)
        n = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES)
        c = pysodium.crypto_box_afternm(m, n, k)
        self.assertEqual(c, c)
        plaintext = pysodium.crypto_box_open_afternm(c, n, k)
        self.assertEqual(m, plaintext)
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
# self specifies the sender for signing the message using pk crypto
    # basedir provides a root for the keystores needed for pk crypto
    # if both self and recipient is specified pk crypto is used, otherwise symmetric
    # this function also handles buffering.
    fd = inputfd(infile)
    outfd = outputfd(outfile or (infile+'.pbp' if infile not in [None,'-'] else '-'))

    if recipient and self:
        # let's do public key encryption
        key = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES)
        me = publickey.Identity(self, basedir=basedir)
        size = struct.pack('>H',len(recipient))
        # write out encrypted message key (nonce, c(key+recplen)) for each recipient
        for r in recipient:
            r = publickey.Identity(r, basedir=basedir, publicOnly=True)
            nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
            outfd.write(nonce)
            outfd.write(nacl.crypto_box(key+size, nonce, r.cp, me.cs))
        me.clear()
    else:
        # let's do symmetric crypto
        key = getkey(nacl.crypto_secretbox_KEYBYTES)

    buf = fd.read(BLOCK_SIZE)
    if buf:
        nonce, cipher = encrypt(buf, k=key)
        outfd.write(nonce)
        outfd.write(cipher)
        buf = fd.read(BLOCK_SIZE)
        while buf:
            nonce = inc_nonce(nonce)
            nonce, cipher = encrypt(buf, k=key, nonce=nonce)
github stef / pbp / publickey.py View on Github external
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
github stef / pbp / pbp / chaining.py View on Github external
def load(self):
        keyfdir="%s/sk/.%s" % (self.basedir, self.me)
        if not os.path.exists(keyfdir):
            os.mkdir(keyfdir)
            return self
        keyfname='%s/%s' % (keyfdir, self.peer)
        if not os.path.exists(keyfname):
            return self
        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)
            plain =  nacl.crypto_box_open(fd.read(), nonce, self.me_id.cp, self.me_id.cs)
        c=nacl.crypto_scalarmult_curve25519_BYTES
        i=0
        self.e_in     = plain[:c]
        i+=c
        self.e_out    = plain[i:i+c]
        i+=c
        self.peer_pub = plain[i:i+c]
        i+=c
        c=nacl.crypto_secretbox_KEYBYTES
        self.out_k    = plain[i:i+c]
        i+=c
        self.in_k     = plain[i:i+c]
        i+=c
        self.in_prev  = plain[i:i+c]
github stef / pbp / pbp / utils.py View on Github external
def inc_nonce(nonce):
    i=0
    nonce = [x for x in nonce]
    while(i
github stef / pbp / pbp / ecdh.py View on Github external
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)
github stef / pbp / pbp / ecdh.py View on Github external
def save(self):
        keyfdir="%s/dh/" % (self.basedir)
        if not os.path.exists(keyfdir):
            os.mkdir(keyfdir)
        keyfdir="%s/%s" % (keyfdir, self.me)
        if not os.path.exists(keyfdir):
            os.mkdir(keyfdir)
        fname='%s/%s' % (keyfdir, self.id)
        nonce = nacl.randombytes(nacl.crypto_box_NONCEBYTES)
        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(self.key, nonce, self.me_id.cp, self.me_id.cs))
github stef / pbp / pbp / publickey.py View on Github external
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