{-# LANGUAGE TypeApplications #-}

-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- Common hashing functions used and re-used across the codebase.

module Crypto.Hash.Utils
    ( blake2b256
    , blake2b224
    ) where

import Prelude

import Crypto.Hash
    ( hash )
import Crypto.Hash.Algorithms
    ( Blake2b_224, Blake2b_256 )
import Data.ByteArray
    ( ByteArrayAccess )
import Data.ByteString
    ( ByteString )

import qualified Data.ByteArray as BA

-- | Hash a byte string using Blake2b with a 256-bit (32-byte) digest.
blake2b256 :: ByteArrayAccess a => a -> ByteString
blake2b256 :: a -> ByteString
blake2b256 = Digest Blake2b_256 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (Digest Blake2b_256 -> ByteString)
-> (a -> Digest Blake2b_256) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteArrayAccess a, HashAlgorithm Blake2b_256) =>
a -> Digest Blake2b_256
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash @_ @Blake2b_256

-- | Hash a byte string using Blake2b with a 224-bit (28-byte) digest.
blake2b224 :: ByteArrayAccess a => a -> ByteString
blake2b224 :: a -> ByteString
blake2b224 = Digest Blake2b_224 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (Digest Blake2b_224 -> ByteString)
-> (a -> Digest Blake2b_224) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteArrayAccess a, HashAlgorithm Blake2b_224) =>
a -> Digest Blake2b_224
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash @_ @Blake2b_224