{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

-- | Implementation of short hashing algorithm, suitable for testing.
module Cardano.Crypto.Hash.Short
  ( ShortHash
  , Blake2bPrefix
  )
where

import Cardano.Crypto.Hash.Class
import Cardano.Crypto.Hash.Blake2b (blake2b_libsodium)

import GHC.TypeLits (Nat, KnownNat, CmpNat, natVal)
import Data.Proxy (Proxy (..))

type ShortHash = Blake2bPrefix 8

data Blake2bPrefix (n :: Nat)

instance (KnownNat n, CmpNat n 33 ~ 'LT) => HashAlgorithm (Blake2bPrefix n) where
  type SizeHash (Blake2bPrefix n) = n
  hashAlgorithmName :: proxy (Blake2bPrefix n) -> String
hashAlgorithmName proxy (Blake2bPrefix n)
_ = String
"blake2b_prefix_" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
  digest :: proxy (Blake2bPrefix n) -> ByteString -> ByteString
digest proxy (Blake2bPrefix n)
_ = Int -> ByteString -> ByteString
blake2b_libsodium (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)))