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

Add nacl.hash, a module for secure message digests

* Supports sha256 and sha512
parent 467747a2
No related branches found
No related tags found
No related merge requests found
from . import hash
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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment