module Botan.Hash.BLAKE
( BLAKE2b(..)
, BLAKE2bDigest(..)
, blake2b
, blake2bLazy
) where
import GHC.TypeLits
import Data.Maybe
import Data.Proxy
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.Text as Text
import qualified Botan.Hash as Botan
import qualified Botan.Utility as Botan
import Botan.Hash.Class
import Botan.Prelude
data BLAKE2b (n :: Nat)
type BLAKE2bSize (n :: Nat) = (KnownNat n, (1 <=? n) ~ True, Mod n 8 ~ 0, (n <=? 512) ~ True)
type BLAKE2bDigest n = Digest (BLAKE2b n)
newtype instance Digest (BLAKE2b n) = BLAKE2bDigest
{ forall (n :: Nat). Digest (BLAKE2b n) -> ByteString
getBLAKE2bByteString :: ByteString }
deriving newtype (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
(Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> Eq (Digest (BLAKE2b n))
forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
== :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
$c/= :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
/= :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
Eq, Eq (Digest (BLAKE2b n))
Eq (Digest (BLAKE2b n)) =>
(Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Ordering)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool)
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n))
-> (Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n))
-> Ord (Digest (BLAKE2b n))
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Ordering
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
forall (n :: Nat). Eq (Digest (BLAKE2b n))
forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
forall (n :: Nat).
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Ordering
forall (n :: Nat).
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: forall (n :: Nat).
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Ordering
compare :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Ordering
$c< :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
< :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
$c<= :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
<= :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
$c> :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
> :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
$c>= :: forall (n :: Nat). Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
>= :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Bool
$cmax :: forall (n :: Nat).
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
max :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
$cmin :: forall (n :: Nat).
Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
min :: Digest (BLAKE2b n) -> Digest (BLAKE2b n) -> Digest (BLAKE2b n)
Ord)
instance Show (Digest (BLAKE2b n)) where
show :: Digest (BLAKE2b n) -> String
show :: Digest (BLAKE2b n) -> String
show (BLAKE2bDigest ByteString
bytes) = Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> HexCase -> Text
Botan.hexEncode ByteString
bytes HexCase
Botan.Lower
instance (BLAKE2bSize n) => Hash (BLAKE2b n) where
hash :: ByteString -> Digest (BLAKE2b n)
hash :: ByteString -> Digest (BLAKE2b n)
hash = ByteString -> Digest (BLAKE2b n)
forall (n :: Nat). ByteString -> Digest (BLAKE2b n)
BLAKE2bDigest (ByteString -> Digest (BLAKE2b n))
-> (ByteString -> ByteString) -> ByteString -> Digest (BLAKE2b n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hash Hash
h where
n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.blake2b Int
n
instance (BLAKE2bSize n) => IncrementalHash (BLAKE2b n) where
hashLazy :: Lazy.ByteString -> Digest (BLAKE2b n)
hashLazy :: ByteString -> Digest (BLAKE2b n)
hashLazy = ByteString -> Digest (BLAKE2b n)
forall (n :: Nat). ByteString -> Digest (BLAKE2b n)
BLAKE2bDigest (ByteString -> Digest (BLAKE2b n))
-> (ByteString -> ByteString) -> ByteString -> Digest (BLAKE2b n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hashLazy Hash
h where
n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.blake2b Int
n
blake2b :: (BLAKE2bSize n) => ByteString -> BLAKE2bDigest n
blake2b :: forall (n :: Nat).
BLAKE2bSize n =>
ByteString -> Digest (BLAKE2b n)
blake2b = ByteString -> Digest (BLAKE2b n)
forall hash. Hash hash => ByteString -> Digest hash
hash
blake2bLazy :: (BLAKE2bSize n) => Lazy.ByteString -> BLAKE2bDigest n
blake2bLazy :: forall (n :: Nat).
BLAKE2bSize n =>
ByteString -> Digest (BLAKE2b n)
blake2bLazy = ByteString -> Digest (BLAKE2b n)
forall hash. IncrementalHash hash => ByteString -> Digest hash
hashLazy