How to use the libnacl.utils.rand_nonce function in libnacl

To help you get started, we’ve selected a few libnacl 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 PowerDNS / pdns / regression-tests.dnsdist / dnscrypt.py View on Github external
def _generateNonce():
        nonce = libnacl.utils.rand_nonce()
        return nonce[:(DNSCryptClient.DNSCRYPT_NONCE_SIZE / 2)]
github saltstack / libnacl / tests / unit / test_raw_public.py View on Github external
def test_box(self):
        msg = b'Are you suggesting coconuts migrate?'
        # run 1
        nonce1 = libnacl.utils.rand_nonce()
        pk1, sk1 = libnacl.crypto_box_keypair()
        pk2, sk2 = libnacl.crypto_box_keypair()
        enc_msg = libnacl.crypto_box(msg, nonce1, pk2, sk1)
        self.assertNotEqual(msg, enc_msg)
        clear_msg = libnacl.crypto_box_open(enc_msg, nonce1, pk1, sk2)
        self.assertEqual(clear_msg, msg)
        # run 2
        nonce2 = libnacl.utils.rand_nonce()
        pk3, sk3 = libnacl.crypto_box_keypair()
        pk4, sk4 = libnacl.crypto_box_keypair()
        enc_msg2 = libnacl.crypto_box(msg, nonce2, pk4, sk3)
        self.assertNotEqual(msg, enc_msg2)
        clear_msg2 = libnacl.crypto_box_open(enc_msg2, nonce2, pk3, sk4)
        self.assertEqual(clear_msg2, msg)
        # Check bits
        self.assertNotEqual(nonce1, nonce2)
        self.assertNotEqual(enc_msg, enc_msg2)
github PowerDNS / pdns / regression-tests.dnsdist / dnsdisttests.py View on Github external
def sendConsoleCommand(cls, command, timeout=1.0):
        ourNonce = libnacl.utils.rand_nonce()
        theirNonce = None
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        if timeout:
            sock.settimeout(timeout)

        sock.connect(("127.0.0.1", cls._consolePort))
        sock.send(ourNonce)
        theirNonce = sock.recv(len(ourNonce))
        if len(theirNonce) != len(ourNonce):
            print("Received a nonce of size %d, expecting %d, console command will not be sent!" % (len(theirNonce), len(ourNonce)))
            if len(theirNonce) == 0:
                raise socket.error("Got EOF while reading a nonce of size %d, console command will not be sent!" % (len(ourNonce)))
            return None

        halfNonceSize = int(len(ourNonce) / 2)
github eavatar / eavatar-me / tests / unit / test_util_crypto.py View on Github external
def test_nonce_generation(self):

        prev_nonce = libnacl.utils.rand_nonce()
        for i in xrange(100):
            nonce = libnacl.utils.rand_nonce()
            self.assertNotEqual(prev_nonce, nonce)
            prev_nonce = nonce
github JoinMarket-Org / joinmarket / lib / libnacl / public.py View on Github external
def encrypt(self, msg, nonce=None, pack_nonce=True):
        '''
        Encrypt the given message with the given nonce, if the nonce is not
        provided it will be generated from the libnacl.utils.rand_nonce
        function
        '''
        if nonce is None:
            nonce = libnacl.utils.rand_nonce()
        elif len(nonce) != libnacl.crypto_box_NONCEBYTES:
            raise ValueError('Invalid nonce size')
        ctxt = libnacl.crypto_box_afternm(msg, nonce, self._k)
        if pack_nonce:
            return nonce + ctxt
        else:
            return nonce, ctxt
github JoinMarket-Org / joinmarket / libnacl / secret.py View on Github external
def encrypt(self, msg, nonce=None):
        '''
        Encrypt the given message. If a nonce is not given it will be
        generated via the rand_nonce function
        '''
        if nonce is None:
            nonce = libnacl.utils.rand_nonce()
        if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
            raise ValueError('Invalid Nonce')
        ctxt = libnacl.crypto_secretbox(msg, nonce, self.sk)
        return nonce + ctxt