diff --git a/src/nacl/_lib/crypto_hash.h b/src/nacl/_lib/crypto_hash.h new file mode 100644 index 0000000000000000000000000000000000000000..9ca6473799a203325f3a1a47c48cf79c13ace25d --- /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 eb584354fa2fdf4830e65da806d6ac0e0e69df81..48b369a40ed57d6dcdc27b31d19be77d17987c71 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 0000000000000000000000000000000000000000..e69cc3e5d683bed7b87579b35a355e36dc0a00c5 --- /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 eb0fdc0dd16438e02f11f9269e3cc487ba1c8eba..61f2f884de9601955a173b8142fc788a9351e962 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))