From a7c217cc8d4e74b2fd74739cace8d13a116d6014 Mon Sep 17 00:00:00 2001 From: Donald Stufft <donald@stufft.io> Date: Mon, 7 Oct 2013 07:50:03 -0400 Subject: [PATCH] Move the hash functions over to the new layout --- src/nacl/_lib/crypto_hash.h | 29 +++++++++++++++++ src/nacl/c/__init__.py | 11 +++++++ src/nacl/c/crypto_hash.py | 62 +++++++++++++++++++++++++++++++++++++ src/nacl/hash.py | 23 ++++---------- 4 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 src/nacl/_lib/crypto_hash.h create mode 100644 src/nacl/c/crypto_hash.py diff --git a/src/nacl/_lib/crypto_hash.h b/src/nacl/_lib/crypto_hash.h new file mode 100644 index 00000000..9ca64737 --- /dev/null +++ b/src/nacl/_lib/crypto_hash.h @@ -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. + */ + + +// size_t crypto_hash_bytes(); +size_t crypto_hash_sha256_bytes(); +size_t crypto_hash_sha512_bytes(); + + +int crypto_hash(unsigned char *out, const unsigned char *in, + unsigned long long inlen); + +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); diff --git a/src/nacl/c/__init__.py b/src/nacl/c/__init__.py index eb584354..48b369a4 100644 --- a/src/nacl/c/__init__.py +++ b/src/nacl/c/__init__.py @@ -19,6 +19,10 @@ from nacl.c.crypto_box import ( crypto_box_BEFORENMBYTES, crypto_box_keypair, crypto_box, crypto_box_open, crypto_box_beforenm, crypto_box_afternm, crypto_box_open_afternm, ) +from nacl.c.crypto_hash import ( + crypto_hash_BYTES, crypto_hash_sha256_BYTES, crypto_hash_sha512_BYTES, + crypto_hash, crypto_hash_sha256, crypto_hash_sha512, +) from nacl.c.crypto_scalarmult import ( crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, crypto_scalarmult_base, @@ -50,6 +54,13 @@ __all__ = [ "crypto_box_afternm", "crypto_box_open_afternm", + "crypto_hash_BYTES", + "crypto_hash_sha256_BYTES", + "crypto_hash_sha512_BYTES", + "crypto_hash", + "crypto_hash_sha256", + "crypto_hash_sha512", + "crypto_scalarmult_BYTES", "crypto_scalarmult_SCALARBYTES", "crypto_scalarmult_base", diff --git a/src/nacl/c/crypto_hash.py b/src/nacl/c/crypto_hash.py new file mode 100644 index 00000000..e69cc3e5 --- /dev/null +++ b/src/nacl/c/crypto_hash.py @@ -0,0 +1,62 @@ +# 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 +from nacl.exceptions import CryptoError + + +# crypto_hash_BYTES = lib.crypto_hash_bytes() +crypto_hash_BYTES = lib.crypto_hash_sha512_bytes() +crypto_hash_sha256_BYTES = lib.crypto_hash_sha256_bytes() +crypto_hash_sha512_BYTES = lib.crypto_hash_sha512_bytes() + + +def crypto_hash(message): + """ + Hashes and returns the message ``message``. + + :param message: bytes + :rtype: bytes + """ + digest = lib.ffi.new("unsigned char[]", crypto_hash_BYTES) + if lib.crypto_hash(digest, message, len(message)) != 0: + raise CryptoError("Hashing failed") + return lib.ffi.buffer(digest, crypto_hash_BYTES)[:] + + +def crypto_hash_sha256(message): + """ + Hashes and returns the message ``message``. + + :param message: bytes + :rtype: bytes + """ + digest = lib.ffi.new("unsigned char[]", crypto_hash_sha256_BYTES) + if lib.crypto_hash_sha256(digest, message, len(message)) != 0: + raise CryptoError("Hashing failed") + return lib.ffi.buffer(digest, crypto_hash_sha256_BYTES)[:] + + +def crypto_hash_sha512(message): + """ + Hashes and returns the message ``message``. + + :param message: bytes + :rtype: bytes + """ + digest = lib.ffi.new("unsigned char[]", crypto_hash_sha512_BYTES) + if lib.crypto_hash_sha512(digest, message, len(message)) != 0: + raise CryptoError("Hashing failed") + return lib.ffi.buffer(digest, crypto_hash_sha512_BYTES)[:] diff --git a/src/nacl/hash.py b/src/nacl/hash.py index eb0fdc0d..61f2f884 100644 --- a/src/nacl/hash.py +++ b/src/nacl/hash.py @@ -14,24 +14,13 @@ from __future__ import absolute_import from __future__ import division -from . import encoding -from .c import _lib as nacl -from .exceptions import CryptoError +import nacl.c +import nacl.encoding -def sha256(message, encoder=encoding.HexEncoder): - digest = nacl.ffi.new("unsigned char[]", nacl.lib.crypto_hash_sha256_BYTES) - if not nacl.lib.crypto_hash_sha256(digest, message, len(message)): - raise CryptoError("Hashing failed") - digest = nacl.ffi.buffer(digest, nacl.lib.crypto_hash_sha256_BYTES)[:] +def sha256(message, encoder=nacl.encoding.HexEncoder): + return encoder.encode(nacl.c.crypto_hash_sha256(message)) - return encoder.encode(digest) - -def sha512(message, encoder=encoding.HexEncoder): - digest = nacl.ffi.new("unsigned char[]", nacl.lib.crypto_hash_sha512_BYTES) - if not nacl.lib.crypto_hash_sha512(digest, message, len(message)): - raise CryptoError("Hashing failed") - digest = nacl.ffi.buffer(digest, nacl.lib.crypto_hash_sha512_BYTES)[:] - - return encoder.encode(digest) +def sha512(message, encoder=nacl.encoding.HexEncoder): + return encoder.encode(nacl.c.crypto_hash_sha512(message)) -- GitLab