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

Move the hash functions over to the new layout

parent 8fa69462
No related branches found
No related tags found
No related merge requests found
/* 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);
...@@ -19,6 +19,10 @@ from nacl.c.crypto_box import ( ...@@ -19,6 +19,10 @@ from nacl.c.crypto_box import (
crypto_box_BEFORENMBYTES, crypto_box_keypair, crypto_box, crypto_box_open, crypto_box_BEFORENMBYTES, crypto_box_keypair, crypto_box, crypto_box_open,
crypto_box_beforenm, crypto_box_afternm, crypto_box_open_afternm, 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 ( from nacl.c.crypto_scalarmult import (
crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES,
crypto_scalarmult_base, crypto_scalarmult_base,
...@@ -50,6 +54,13 @@ __all__ = [ ...@@ -50,6 +54,13 @@ __all__ = [
"crypto_box_afternm", "crypto_box_afternm",
"crypto_box_open_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_BYTES",
"crypto_scalarmult_SCALARBYTES", "crypto_scalarmult_SCALARBYTES",
"crypto_scalarmult_base", "crypto_scalarmult_base",
......
# 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)[:]
...@@ -14,24 +14,13 @@ ...@@ -14,24 +14,13 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
from . import encoding import nacl.c
from .c import _lib as nacl import nacl.encoding
from .exceptions import CryptoError
def sha256(message, encoder=encoding.HexEncoder): def sha256(message, encoder=nacl.encoding.HexEncoder):
digest = nacl.ffi.new("unsigned char[]", nacl.lib.crypto_hash_sha256_BYTES) return encoder.encode(nacl.c.crypto_hash_sha256(message))
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)[:]
return encoder.encode(digest)
def sha512(message, encoder=nacl.encoding.HexEncoder):
def sha512(message, encoder=encoding.HexEncoder): return encoder.encode(nacl.c.crypto_hash_sha512(message))
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment