Skip to content
Snippets Groups Projects
Commit b2d863c4 authored by Donald Stufft's avatar Donald Stufft
Browse files

Add nacl.random(N) to generate a random N bytes

parent fbe68012
No related branches found
No related tags found
No related merge requests found
from . import __about__
from . import hash # pylint: disable=W0622
from .random import random
__all__ = ["hash"] + __about__.__all__
__all__ = ["hash", "random"] + __about__.__all__
# - Meta Information -
......
......@@ -23,6 +23,11 @@ ffi.cdef(
int crypto_hash_sha256(unsigned char *out, const unsigned char *in, unsigned long long inlen);
int crypto_hash_sha512(unsigned char *out, const unsigned char *in, unsigned long long inlen);
"""
# Secure Random
"""
void randombytes(unsigned char * const buf, const unsigned long long buf_len);
"""
)
......@@ -37,6 +42,11 @@ try:
#include "crypto_hash.h"
#include "crypto_hash_sha256.h"
#include "crypto_hash_sha512.h"
"""
# Secure Random
"""
#include "randombytes.h"
""",
libraries=["nacl"],
)
......
from . import nacl
def random(size=32):
data = nacl.ffi.new("unsigned char[]", size)
nacl.lib.randombytes(data, size)
return nacl.ffi.string(data)
import nacl
def test_random_bytes_produces():
assert len(nacl.random(16)) == 16
def test_random_bytes_produces_different_bytes():
assert nacl.random(16) != nacl.random(16)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment