From 2a35b6b3a495af7c7bc083bb1a0741cafa5d86c3 Mon Sep 17 00:00:00 2001 From: Donald Stufft <donald@stufft.io> Date: Sun, 6 Oct 2013 22:00:35 -0400 Subject: [PATCH] Move the random implementation over to the new layout --- src/nacl/_lib/randombytes.h | 16 ++++++++++++++++ src/nacl/c/__init__.py | 3 +++ src/nacl/c/randombytes.py | 29 +++++++++++++++++++++++++++++ src/nacl/utils.py | 6 ++---- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/nacl/_lib/randombytes.h create mode 100644 src/nacl/c/randombytes.py diff --git a/src/nacl/_lib/randombytes.h b/src/nacl/_lib/randombytes.h new file mode 100644 index 00000000..6952fbbe --- /dev/null +++ b/src/nacl/_lib/randombytes.h @@ -0,0 +1,16 @@ +/* Copyright 2013 Donald Stufft and individual contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +void randombytes(unsigned char * const buf, const unsigned long long buf_len); diff --git a/src/nacl/c/__init__.py b/src/nacl/c/__init__.py index e9ef7f97..e062935a 100644 --- a/src/nacl/c/__init__.py +++ b/src/nacl/c/__init__.py @@ -23,6 +23,7 @@ from nacl.c.crypto_scalarmult import ( crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, crypto_scalarmult_base, ) +from nacl.c.randombytes import randombytes __all__ = [ @@ -42,4 +43,6 @@ __all__ = [ "crypto_scalarmult_BYTES", "crypto_scalarmult_SCALARBYTES", "crypto_scalarmult_base", + + "randombytes", ] diff --git a/src/nacl/c/randombytes.py b/src/nacl/c/randombytes.py new file mode 100644 index 00000000..cee7a5e3 --- /dev/null +++ b/src/nacl/c/randombytes.py @@ -0,0 +1,29 @@ +# Copyright 2013 Donald Stufft and individual contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import absolute_import, division, print_function + +from nacl import _lib as lib + + +def randombytes(size): + """ + Returns ``size`` number of random bytes from a cryptographically secure + random source. + + :param size: int + :rtype: bytes + """ + buf = lib.ffi.new("unsigned char[]", size) + lib.randombytes(buf, size) + return lib.ffi.buffer(buf, size)[:] diff --git a/src/nacl/utils.py b/src/nacl/utils.py index 963b7d6c..61b901cb 100644 --- a/src/nacl/utils.py +++ b/src/nacl/utils.py @@ -16,7 +16,7 @@ from __future__ import division import six -from .c import _lib as nacl +import nacl.c class EncryptedMessage(six.binary_type): @@ -57,6 +57,4 @@ class StringFixer(object): def random(size=32): - data = nacl.ffi.new("unsigned char[]", size) - nacl.lib.randombytes(data, size) - return nacl.ffi.buffer(data, size)[:] + return nacl.c.randombytes(size) -- GitLab