From 30c4ef18e4bc8e8e0b374b97c52eaf71caf2ff9c Mon Sep 17 00:00:00 2001
From: Donald Stufft <donald.stufft@gmail.com>
Date: Fri, 22 Feb 2013 00:42:28 -0500
Subject: [PATCH] Add nacl.hash, a module for secure message digests

* Supports sha256 and sha512
---
 nacl/__init__.py |  1 +
 nacl/hash.py     | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 nacl/hash.py

diff --git a/nacl/__init__.py b/nacl/__init__.py
index e69de29b..eca463aa 100644
--- a/nacl/__init__.py
+++ b/nacl/__init__.py
@@ -0,0 +1 @@
+from . import hash
diff --git a/nacl/hash.py b/nacl/hash.py
new file mode 100644
index 00000000..9ebe0cfc
--- /dev/null
+++ b/nacl/hash.py
@@ -0,0 +1,26 @@
+import binascii
+
+from . import nacl
+from .exceptions import CryptoError
+
+
+def sha256(message, binary=False):
+    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.string(digest)
+
+    if binary:
+        return digest
+    return binascii.hexlify(digest).decode("ascii")
+
+
+def sha512(message, binary=False):
+    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.string(digest)
+
+    if binary:
+        return digest
+    return binascii.hexlify(digest).decode("ascii")
-- 
GitLab