{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}

-- | Implementation of the SHA256 hashing algorithm.
module Cardano.Crypto.Hash.SHA256 (
  SHA256,
)
where

import Cardano.Crypto.Hash.Class (HashAlgorithm, SizeHash, digest, hashAlgorithmName)
import Cardano.Crypto.Libsodium.C (c_crypto_hash_sha256)
import Cardano.Foreign (SizedPtr (SizedPtr))
import Control.Monad (unless)

import Data.Proxy (Proxy (..))
import Foreign.C.Error (errnoToIOError, getErrno)
import Foreign.Ptr (castPtr)
import GHC.IO.Exception (ioException)
import GHC.TypeLits (natVal)

import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as BI

data SHA256

instance HashAlgorithm SHA256 where
  type SizeHash SHA256 = 32
  hashAlgorithmName :: forall (proxy :: * -> *). proxy SHA256 -> String
hashAlgorithmName proxy SHA256
_ = String
"sha256"
  digest :: forall (proxy :: * -> *). proxy SHA256 -> ByteString -> ByteString
digest proxy SHA256
_ = ByteString -> ByteString
sha256_libsodium

sha256_libsodium :: B.ByteString -> B.ByteString
sha256_libsodium :: ByteString -> ByteString
sha256_libsodium ByteString
input =
  Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
expected_size forall a b. (a -> b) -> a -> b
$ \Ptr Word8
outptr ->
    forall a. ByteString -> (CStringLen -> IO a) -> IO a
B.useAsCStringLen ByteString
input forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
inptr, Int
inputlen) -> do
      Int
res <- SizedPtr 32 -> Ptr CUChar -> CULLong -> IO Int
c_crypto_hash_sha256 (forall (n :: Nat). Ptr Void -> SizedPtr n
SizedPtr (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
outptr)) (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
inptr) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
inputlen)
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
res forall a. Eq a => a -> a -> Bool
== Int
0) forall a b. (a -> b) -> a -> b
$ do
        Errno
errno <- IO Errno
getErrno
        forall a. IOException -> IO a
ioException forall a b. (a -> b) -> a -> b
$ String -> Errno -> Maybe Handle -> Maybe String -> IOException
errnoToIOError String
"digest @SHA256: c_crypto_hash_sha256" Errno
errno forall a. Maybe a
Nothing forall a. Maybe a
Nothing
  where
    expected_size :: Int
expected_size = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy (SizeHash SHA256)))