Skip to content

Instantly share code, notes, and snippets.

@creichert
Created April 18, 2017 23:15
Show Gist options
  • Save creichert/cf95ca7942198423a234e628a083d692 to your computer and use it in GitHub Desktop.
Save creichert/cf95ca7942198423a234e628a083d692 to your computer and use it in GitHub Desktop.
Example hashing functions using `cryptonite`
{-# LANGUAGE PackageImports #-}
module Crypto.Assertible
(
sha1Hex
, sha1HmacHex
, sha256Hash
) where
import "cryptonite" Crypto.Hash as Crypto
import "cryptonite" Crypto.MAC.HMAC as Crypto
import Data.ByteArray as Memory
import Data.ByteArray.Encoding as Memory
import Data.ByteString
sha1Hex :: ByteString -> ByteString
sha1Hex s = digestToHexByteString (hash s :: Digest SHA1)
sha1HmacHex :: ByteString -> ByteString -> ByteString
sha1HmacHex sec content = digestToHexByteString
(Crypto.hmacGetDigest (Crypto.hmac sec content :: HMAC SHA1))
sha256Hash :: ByteString -> ByteString
sha256Hash s = digestToByteString (hash s :: Digest SHA256)
-- | Return the hexadecimal (base16) bytestring of the digest
--
-- See 'Data.ByteArray.convert' or 'Data.ByteArray.Encoding.Base' for
-- other encodings
digestToHexByteString :: Digest a -> ByteString
digestToHexByteString = Memory.convertToBase Memory.Base16
digestToByteString :: Digest a -> ByteString
digestToByteString = Memory.convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment