How to use the libnacl.randombytes 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 hyperledger / indy-plenum / plenum / common / util.py View on Github external
def randomStr(size):
        if not isinstance(size, int):
            raise PlenumTypeError('size', size, int)
        if not size > 0:
            raise PlenumValueError('size', size, '> 0')
        # Approach 1
        rv = randombytes(size // 2).hex()

        return rv if size % 2 == 0 else rv + hex(randombytes_uniform(15))[-1]
github hyperledger / indy-node / scripts / performance / perf_load / perf_utils.py View on Github external
def random_string(sz: int) -> str:
    assert (sz > 0), "Expected random string size cannot be less than 1"
    rv = libnacl.randombytes(sz // 2).hex()
    return rv if sz % 2 == 0 else rv + hex(libnacl.randombytes_uniform(15))[-1]
github saltstack / libnacl / tests / unit / test_auth_verify.py View on Github external
def test_onetimeauth_verify(self):
        self.assertEqual("poly1305", libnacl.crypto_onetimeauth_primitive())

        msg = b'Anybody can invent a cryptosystem he cannot break himself. Except Bruce Schneier.'
        key1 = libnacl.randombytes(libnacl.crypto_onetimeauth_KEYBYTES)
        key2 = libnacl.randombytes(libnacl.crypto_onetimeauth_KEYBYTES)

        sig1 = libnacl.crypto_onetimeauth(msg, key1)
        sig2 = libnacl.crypto_onetimeauth(msg, key2)

        with self.assertRaises(ValueError):
            libnacl.crypto_onetimeauth(msg, b'too_short')

        with self.assertRaises(ValueError):
            libnacl.crypto_onetimeauth_verify(sig1, msg, b'too_short')

        with self.assertRaises(ValueError):
            libnacl.crypto_onetimeauth_verify(b'too_short', msg, key1)

        self.assertTrue(libnacl.crypto_onetimeauth_verify(sig1, msg, key1))
        self.assertTrue(libnacl.crypto_onetimeauth_verify(sig2, msg, key2))
github saltstack / libnacl / libnacl / utils.py View on Github external
def salsa_key():
    '''
    Generates a salsa2020 key
    '''
    return libnacl.randombytes(libnacl.crypto_secretbox_KEYBYTES)
github hyperledger / indy-plenum / stp_core / crypto / nacl_wrappers.py View on Github external
def nonce(self):
        '''
        Generate a safe nonce value (safe assuming only this method is used to
        create nonce values)
        '''
        return libnacl.randombytes(Box.NONCE_SIZE)
github JoinMarket-Org / joinmarket / lib / libnacl / utils.py View on Github external
def rand_nonce():
    '''
    Generates and returns a random bytestring of the size defined in libsodium
    as crypto_box_NONCEBYTES
    '''
    return libnacl.randombytes(libnacl.crypto_box_NONCEBYTES)
github hyperledger / indy-node / indy_client / cli / cli.py View on Github external
def _change_current_key(self, seed=None):
        if not self.isValidSeedForNewKey(seed):
            return True

        cur_id = self.activeWallet.requiredIdr()
        cseed = cleanSeed(seed or randombytes(32))

        dm = self.activeWallet.didMethods.get(None)
        signer = dm.newSigner(identifier=cur_id, seed=cseed)

        def change_verkey_cb(reply, error, *args, **kwargs):
            if error:
                self.print("Error: {}".format(error), Token.BoldBlue)
            else:
                self.activeWallet.updateSigner(cur_id, signer)
                self._saveActiveWallet()
                self.print("Key changed for {}".format(
                    reply[TARGET_NYM]), Token.BoldBlue)
                self.print("New verification key is {}".format(
                    signer.verkey), Token.BoldBlue)

        self._addNym(nym=cur_id, role=None, newVerKey=signer.verkey,
github hyperledger / indy-plenum / plenum / common / signer_simple.py View on Github external
def __init__(self, identifier=None, seed=None, alias=None):
        """
        Initialize the signer with an identifier and a seed.

        :param identifier: some identifier that directly or indirectly
        references this client
        :param seed: the seed used to generate a signing key.
        """

        # should be stored securely/privately
        self.seed = seed if seed else randombytes(32)

        # generates key pair based on seed
        self.sk = SigningKey(seed=self.seed)

        # helper for signing
        self.naclSigner = NaclSigner(self.sk)

        # this is the public key used to verify signatures (securely shared
        # before-hand with recipient)
        hex_verkey = hexlify(self.naclSigner.verraw)
        self.verkey = hexToFriendly(hex_verkey)

        self._identifier = identifier or self.verkey

        self._alias = alias