{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, DeriveGeneric, ForeignFunctionInterface #-}
-- |
-- Module      : Crypto.Saltine.Internal.Hash
-- Copyright   : (c) Max Amanshauser 2021
-- License     : MIT
--
-- Maintainer  : max@lambdalifting.org
-- Stability   : experimental
-- Portability : non-portable
--
module Crypto.Saltine.Internal.Hash (
    hash_bytes
  , shorthash_bytes
  , shorthash_keybytes
  , generichash_bytes_max
  , generichash_keybytes_max
  , c_hash
  , c_shorthash
  , c_generichash
  , nullShKey
  , shorthash
  , ShorthashKey(..)
  , GenerichashKey(..)
  , GenerichashOutLen(..)
) where

import Control.DeepSeq
import Crypto.Saltine.Class
import Crypto.Saltine.Internal.Util as U
import Data.ByteString              (ByteString)
import Data.Data                    (Data, Typeable)
import Data.Hashable                (Hashable)
import Data.Monoid
import Foreign.C
import Foreign.Ptr
import GHC.Generics                 (Generic)

import qualified Data.ByteString       as S
import qualified Data.ByteString.Char8 as S8

-- | An opaque 'shorthash' cryptographic secret key.
newtype ShorthashKey = ShK { ShorthashKey -> ByteString
unShK :: ByteString } deriving (Eq ShorthashKey
Eq ShorthashKey
-> (ShorthashKey -> ShorthashKey -> Ordering)
-> (ShorthashKey -> ShorthashKey -> Bool)
-> (ShorthashKey -> ShorthashKey -> Bool)
-> (ShorthashKey -> ShorthashKey -> Bool)
-> (ShorthashKey -> ShorthashKey -> Bool)
-> (ShorthashKey -> ShorthashKey -> ShorthashKey)
-> (ShorthashKey -> ShorthashKey -> ShorthashKey)
-> Ord ShorthashKey
ShorthashKey -> ShorthashKey -> Bool
ShorthashKey -> ShorthashKey -> Ordering
ShorthashKey -> ShorthashKey -> ShorthashKey
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
min :: ShorthashKey -> ShorthashKey -> ShorthashKey
$cmin :: ShorthashKey -> ShorthashKey -> ShorthashKey
max :: ShorthashKey -> ShorthashKey -> ShorthashKey
$cmax :: ShorthashKey -> ShorthashKey -> ShorthashKey
>= :: ShorthashKey -> ShorthashKey -> Bool
$c>= :: ShorthashKey -> ShorthashKey -> Bool
> :: ShorthashKey -> ShorthashKey -> Bool
$c> :: ShorthashKey -> ShorthashKey -> Bool
<= :: ShorthashKey -> ShorthashKey -> Bool
$c<= :: ShorthashKey -> ShorthashKey -> Bool
< :: ShorthashKey -> ShorthashKey -> Bool
$c< :: ShorthashKey -> ShorthashKey -> Bool
compare :: ShorthashKey -> ShorthashKey -> Ordering
$ccompare :: ShorthashKey -> ShorthashKey -> Ordering
$cp1Ord :: Eq ShorthashKey
Ord, Int -> ShorthashKey -> Int
ShorthashKey -> Int
(Int -> ShorthashKey -> Int)
-> (ShorthashKey -> Int) -> Hashable ShorthashKey
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: ShorthashKey -> Int
$chash :: ShorthashKey -> Int
hashWithSalt :: Int -> ShorthashKey -> Int
$chashWithSalt :: Int -> ShorthashKey -> Int
Hashable, Typeable ShorthashKey
DataType
Constr
Typeable ShorthashKey
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ShorthashKey)
-> (ShorthashKey -> Constr)
-> (ShorthashKey -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ShorthashKey))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ShorthashKey))
-> ((forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r)
-> (forall u. (forall d. Data d => d -> u) -> ShorthashKey -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey)
-> Data ShorthashKey
ShorthashKey -> DataType
ShorthashKey -> Constr
(forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u
forall u. (forall d. Data d => d -> u) -> ShorthashKey -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ShorthashKey)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ShorthashKey)
$cShK :: Constr
$tShorthashKey :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
gmapMp :: (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
gmapM :: (forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
gmapQi :: Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u
gmapQ :: (forall d. Data d => d -> u) -> ShorthashKey -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ShorthashKey -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
gmapT :: (forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey
$cgmapT :: (forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ShorthashKey)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ShorthashKey)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c ShorthashKey)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ShorthashKey)
dataTypeOf :: ShorthashKey -> DataType
$cdataTypeOf :: ShorthashKey -> DataType
toConstr :: ShorthashKey -> Constr
$ctoConstr :: ShorthashKey -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
$cp1Data :: Typeable ShorthashKey
Data, Typeable, (forall x. ShorthashKey -> Rep ShorthashKey x)
-> (forall x. Rep ShorthashKey x -> ShorthashKey)
-> Generic ShorthashKey
forall x. Rep ShorthashKey x -> ShorthashKey
forall x. ShorthashKey -> Rep ShorthashKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ShorthashKey x -> ShorthashKey
$cfrom :: forall x. ShorthashKey -> Rep ShorthashKey x
Generic, ShorthashKey -> ()
(ShorthashKey -> ()) -> NFData ShorthashKey
forall a. (a -> ()) -> NFData a
rnf :: ShorthashKey -> ()
$crnf :: ShorthashKey -> ()
NFData)
instance Eq ShorthashKey where
    ShK ByteString
a == :: ShorthashKey -> ShorthashKey -> Bool
== ShK ByteString
b = ByteString -> ByteString -> Bool
U.compare ByteString
a ByteString
b
instance Show ShorthashKey where
    show :: ShorthashKey -> String
show ShorthashKey
k = String
"Hash.ShorthashKey {hashesTo = \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (ByteString -> String
bin2hex (ByteString -> String)
-> (ByteString -> ByteString) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShorthashKey -> ByteString -> ByteString
shorthash ShorthashKey
nullShKey (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ShorthashKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode ShorthashKey
k) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"}\""

-- | Used for our `Show` instances
nullShKey :: ShorthashKey
nullShKey :: ShorthashKey
nullShKey = ByteString -> ShorthashKey
ShK (Int -> Char -> ByteString
S8.replicate Int
shorthash_keybytes Char
'\NUL')

-- | Computes a very short, fast keyed hash.
-- This function is defined here to break circulat module imports
shorthash :: ShorthashKey
          -> ByteString
          -- ^ Message
          -> ByteString
          -- ^ Hash
shorthash :: ShorthashKey -> ByteString -> ByteString
shorthash (ShK ByteString
k) ByteString
m = (CInt, ByteString) -> ByteString
forall a b. (a, b) -> b
snd ((CInt, ByteString) -> ByteString)
-> ((Ptr CChar -> IO CInt) -> (CInt, ByteString))
-> (Ptr CChar -> IO CInt)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (Ptr CChar -> IO CInt) -> (CInt, ByteString)
forall b. Int -> (Ptr CChar -> IO b) -> (b, ByteString)
buildUnsafeByteString Int
shorthash_bytes ((Ptr CChar -> IO CInt) -> ByteString)
-> (Ptr CChar -> IO CInt) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
ph ->
  [ByteString] -> ([CStringLen] -> IO CInt) -> IO CInt
forall b. [ByteString] -> ([CStringLen] -> IO b) -> IO b
constByteStrings [ByteString
k, ByteString
m] (([CStringLen] -> IO CInt) -> IO CInt)
-> ([CStringLen] -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \[(Ptr CChar
pk, Int
_), (Ptr CChar
pm, Int
_)] ->
    Ptr CChar -> Ptr CChar -> CULLong -> Ptr CChar -> IO CInt
c_shorthash Ptr CChar
ph Ptr CChar
pm (Int -> CULLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CULLong) -> Int -> CULLong
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
S.length ByteString
m) Ptr CChar
pk

instance IsEncoding ShorthashKey where
  decode :: ByteString -> Maybe ShorthashKey
decode ByteString
v = if ByteString -> Int
S.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
shorthash_keybytes
           then ShorthashKey -> Maybe ShorthashKey
forall a. a -> Maybe a
Just (ByteString -> ShorthashKey
ShK ByteString
v)
           else Maybe ShorthashKey
forall a. Maybe a
Nothing
  {-# INLINE decode #-}
  encode :: ShorthashKey -> ByteString
encode (ShK ByteString
v) = ByteString
v
  {-# INLINE encode #-}

-- | An opaque 'generichash' cryptographic secret key.
newtype GenerichashKey = GhK { GenerichashKey -> ByteString
unGhK :: ByteString } deriving (Eq GenerichashKey
Eq GenerichashKey
-> (GenerichashKey -> GenerichashKey -> Ordering)
-> (GenerichashKey -> GenerichashKey -> Bool)
-> (GenerichashKey -> GenerichashKey -> Bool)
-> (GenerichashKey -> GenerichashKey -> Bool)
-> (GenerichashKey -> GenerichashKey -> Bool)
-> (GenerichashKey -> GenerichashKey -> GenerichashKey)
-> (GenerichashKey -> GenerichashKey -> GenerichashKey)
-> Ord GenerichashKey
GenerichashKey -> GenerichashKey -> Bool
GenerichashKey -> GenerichashKey -> Ordering
GenerichashKey -> GenerichashKey -> GenerichashKey
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
min :: GenerichashKey -> GenerichashKey -> GenerichashKey
$cmin :: GenerichashKey -> GenerichashKey -> GenerichashKey
max :: GenerichashKey -> GenerichashKey -> GenerichashKey
$cmax :: GenerichashKey -> GenerichashKey -> GenerichashKey
>= :: GenerichashKey -> GenerichashKey -> Bool
$c>= :: GenerichashKey -> GenerichashKey -> Bool
> :: GenerichashKey -> GenerichashKey -> Bool
$c> :: GenerichashKey -> GenerichashKey -> Bool
<= :: GenerichashKey -> GenerichashKey -> Bool
$c<= :: GenerichashKey -> GenerichashKey -> Bool
< :: GenerichashKey -> GenerichashKey -> Bool
$c< :: GenerichashKey -> GenerichashKey -> Bool
compare :: GenerichashKey -> GenerichashKey -> Ordering
$ccompare :: GenerichashKey -> GenerichashKey -> Ordering
$cp1Ord :: Eq GenerichashKey
Ord, Int -> GenerichashKey -> Int
GenerichashKey -> Int
(Int -> GenerichashKey -> Int)
-> (GenerichashKey -> Int) -> Hashable GenerichashKey
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: GenerichashKey -> Int
$chash :: GenerichashKey -> Int
hashWithSalt :: Int -> GenerichashKey -> Int
$chashWithSalt :: Int -> GenerichashKey -> Int
Hashable, Typeable GenerichashKey
DataType
Constr
Typeable GenerichashKey
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c GenerichashKey)
-> (GenerichashKey -> Constr)
-> (GenerichashKey -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c GenerichashKey))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c GenerichashKey))
-> ((forall b. Data b => b -> b)
    -> GenerichashKey -> GenerichashKey)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> GenerichashKey -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> GenerichashKey -> m GenerichashKey)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GenerichashKey -> m GenerichashKey)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GenerichashKey -> m GenerichashKey)
-> Data GenerichashKey
GenerichashKey -> DataType
GenerichashKey -> Constr
(forall b. Data b => b -> b) -> GenerichashKey -> GenerichashKey
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u
forall u. (forall d. Data d => d -> u) -> GenerichashKey -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashKey)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashKey)
$cGhK :: Constr
$tGenerichashKey :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
gmapMp :: (forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
gmapM :: (forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
gmapQi :: Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u
gmapQ :: (forall d. Data d => d -> u) -> GenerichashKey -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashKey -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
gmapT :: (forall b. Data b => b -> b) -> GenerichashKey -> GenerichashKey
$cgmapT :: (forall b. Data b => b -> b) -> GenerichashKey -> GenerichashKey
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashKey)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashKey)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c GenerichashKey)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashKey)
dataTypeOf :: GenerichashKey -> DataType
$cdataTypeOf :: GenerichashKey -> DataType
toConstr :: GenerichashKey -> Constr
$ctoConstr :: GenerichashKey -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
$cp1Data :: Typeable GenerichashKey
Data, Typeable, (forall x. GenerichashKey -> Rep GenerichashKey x)
-> (forall x. Rep GenerichashKey x -> GenerichashKey)
-> Generic GenerichashKey
forall x. Rep GenerichashKey x -> GenerichashKey
forall x. GenerichashKey -> Rep GenerichashKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GenerichashKey x -> GenerichashKey
$cfrom :: forall x. GenerichashKey -> Rep GenerichashKey x
Generic, GenerichashKey -> ()
(GenerichashKey -> ()) -> NFData GenerichashKey
forall a. (a -> ()) -> NFData a
rnf :: GenerichashKey -> ()
$crnf :: GenerichashKey -> ()
NFData)
instance Eq GenerichashKey where
    GhK ByteString
a == :: GenerichashKey -> GenerichashKey -> Bool
== GhK ByteString
b = ByteString -> ByteString -> Bool
U.compare ByteString
a ByteString
b
instance Show GenerichashKey where
    show :: GenerichashKey -> String
show GenerichashKey
k = String
"Hash.GenerichashKey {hashesTo = \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (ByteString -> String
bin2hex (ByteString -> String)
-> (ByteString -> ByteString) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShorthashKey -> ByteString -> ByteString
shorthash ShorthashKey
nullShKey (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ GenerichashKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode GenerichashKey
k) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"}\""

instance IsEncoding GenerichashKey where
  decode :: ByteString -> Maybe GenerichashKey
decode ByteString
v = if ByteString -> Int
S.length ByteString
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
generichash_keybytes_max
             then GenerichashKey -> Maybe GenerichashKey
forall a. a -> Maybe a
Just (ByteString -> GenerichashKey
GhK ByteString
v)
             else Maybe GenerichashKey
forall a. Maybe a
Nothing
  {-# INLINE decode #-}
  encode :: GenerichashKey -> ByteString
encode (GhK ByteString
v) = ByteString
v
  {-# INLINE encode #-}

newtype GenerichashOutLen = GhOL { GenerichashOutLen -> Int
unGhOL :: Int } deriving (GenerichashOutLen -> GenerichashOutLen -> Bool
(GenerichashOutLen -> GenerichashOutLen -> Bool)
-> (GenerichashOutLen -> GenerichashOutLen -> Bool)
-> Eq GenerichashOutLen
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c/= :: GenerichashOutLen -> GenerichashOutLen -> Bool
== :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c== :: GenerichashOutLen -> GenerichashOutLen -> Bool
Eq, Eq GenerichashOutLen
Eq GenerichashOutLen
-> (GenerichashOutLen -> GenerichashOutLen -> Ordering)
-> (GenerichashOutLen -> GenerichashOutLen -> Bool)
-> (GenerichashOutLen -> GenerichashOutLen -> Bool)
-> (GenerichashOutLen -> GenerichashOutLen -> Bool)
-> (GenerichashOutLen -> GenerichashOutLen -> Bool)
-> (GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen)
-> (GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen)
-> Ord GenerichashOutLen
GenerichashOutLen -> GenerichashOutLen -> Bool
GenerichashOutLen -> GenerichashOutLen -> Ordering
GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
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
min :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
$cmin :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
max :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
$cmax :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
>= :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c>= :: GenerichashOutLen -> GenerichashOutLen -> Bool
> :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c> :: GenerichashOutLen -> GenerichashOutLen -> Bool
<= :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c<= :: GenerichashOutLen -> GenerichashOutLen -> Bool
< :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c< :: GenerichashOutLen -> GenerichashOutLen -> Bool
compare :: GenerichashOutLen -> GenerichashOutLen -> Ordering
$ccompare :: GenerichashOutLen -> GenerichashOutLen -> Ordering
$cp1Ord :: Eq GenerichashOutLen
Ord, Int -> GenerichashOutLen -> Int
GenerichashOutLen -> Int
(Int -> GenerichashOutLen -> Int)
-> (GenerichashOutLen -> Int) -> Hashable GenerichashOutLen
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: GenerichashOutLen -> Int
$chash :: GenerichashOutLen -> Int
hashWithSalt :: Int -> GenerichashOutLen -> Int
$chashWithSalt :: Int -> GenerichashOutLen -> Int
Hashable, Typeable GenerichashOutLen
DataType
Constr
Typeable GenerichashOutLen
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> GenerichashOutLen
    -> c GenerichashOutLen)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c GenerichashOutLen)
-> (GenerichashOutLen -> Constr)
-> (GenerichashOutLen -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c GenerichashOutLen))
-> ((forall b. Data b => b -> b)
    -> GenerichashOutLen -> GenerichashOutLen)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> GenerichashOutLen -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> GenerichashOutLen -> m GenerichashOutLen)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GenerichashOutLen -> m GenerichashOutLen)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GenerichashOutLen -> m GenerichashOutLen)
-> Data GenerichashOutLen
GenerichashOutLen -> DataType
GenerichashOutLen -> Constr
(forall b. Data b => b -> b)
-> GenerichashOutLen -> GenerichashOutLen
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u
forall u. (forall d. Data d => d -> u) -> GenerichashOutLen -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashOutLen)
$cGhOL :: Constr
$tGenerichashOutLen :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
gmapMp :: (forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
gmapM :: (forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
gmapQi :: Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u
gmapQ :: (forall d. Data d => d -> u) -> GenerichashOutLen -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashOutLen -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
gmapT :: (forall b. Data b => b -> b)
-> GenerichashOutLen -> GenerichashOutLen
$cgmapT :: (forall b. Data b => b -> b)
-> GenerichashOutLen -> GenerichashOutLen
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashOutLen)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashOutLen)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen)
dataTypeOf :: GenerichashOutLen -> DataType
$cdataTypeOf :: GenerichashOutLen -> DataType
toConstr :: GenerichashOutLen -> Constr
$ctoConstr :: GenerichashOutLen -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
$cp1Data :: Typeable GenerichashOutLen
Data, Typeable, (forall x. GenerichashOutLen -> Rep GenerichashOutLen x)
-> (forall x. Rep GenerichashOutLen x -> GenerichashOutLen)
-> Generic GenerichashOutLen
forall x. Rep GenerichashOutLen x -> GenerichashOutLen
forall x. GenerichashOutLen -> Rep GenerichashOutLen x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GenerichashOutLen x -> GenerichashOutLen
$cfrom :: forall x. GenerichashOutLen -> Rep GenerichashOutLen x
Generic, GenerichashOutLen -> ()
(GenerichashOutLen -> ()) -> NFData GenerichashOutLen
forall a. (a -> ()) -> NFData a
rnf :: GenerichashOutLen -> ()
$crnf :: GenerichashOutLen -> ()
NFData)

hash_bytes, shorthash_bytes, shorthash_keybytes, generichash_bytes_max, generichash_keybytes_max :: Int

-- Hashes
-- | The size of a hash resulting from
-- 'Crypto.Saltine.Internal.Hash.hash'.
hash_bytes :: Int
hash_bytes         = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_hash_bytes
-- | The size of a keyed hash resulting from
-- 'Crypto.Saltine.Internal.Hash.shorthash'.
shorthash_bytes :: Int
shorthash_bytes    = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_shorthash_bytes
-- | The size of a hashing key for the keyed hash function
-- 'Crypto.Saltine.Internal.Hash.shorthash'.
shorthash_keybytes :: Int
shorthash_keybytes = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_shorthash_keybytes
-- | The maximum output size of the generic hash function
-- 'Crypto.Saltine.Core.Hash.generichash'
generichash_bytes_max :: Int
generichash_bytes_max = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_generichash_bytes_max
-- | The maximum key size of the generic hash function
-- 'Crypto.Saltine.Core.Hash.generichash'
generichash_keybytes_max :: Int
generichash_keybytes_max = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_generichash_keybytes_max

-- src/libsodium/crypto_generichash/crypto_generichash.c
foreign import ccall "crypto_generichash_bytes_max"
  c_crypto_generichash_bytes_max :: CSize
foreign import ccall "crypto_generichash_keybytes_max"
  c_crypto_generichash_keybytes_max :: CSize

-- src/libsodium/crypto_hash/crypto_hash.c
-- src/libsodium/include/sodium/crypto_hash.h
foreign import ccall "crypto_hash_bytes"
  c_crypto_hash_bytes :: CSize

-- src/libsodium/crypto_shorthash/crypto_shorthash.c
-- src/libsodium/include/sodium/crypto_shorthash.h
foreign import ccall "crypto_shorthash_bytes"
  c_crypto_shorthash_bytes :: CSize
foreign import ccall "crypto_shorthash_keybytes"
  c_crypto_shorthash_keybytes :: CSize


foreign import ccall "crypto_hash"
  c_hash :: Ptr CChar
         -- ^ Output hash buffer
         -> Ptr CChar
         -- ^ Constant message buffer
         -> CULLong
         -- ^ Constant message buffer length
         -> IO CInt
         -- ^ Always 0

foreign import ccall "crypto_shorthash"
  c_shorthash :: Ptr CChar
              -- ^ Output hash buffer
              -> Ptr CChar
              -- ^ Constant message buffer
              -> CULLong
              -- ^ Message buffer length
              -> Ptr CChar
              -- ^ Constant Key buffer
              -> IO CInt
              -- ^ Always 0

foreign import ccall "crypto_generichash"
  c_generichash :: Ptr CChar
                -- ^ Output hash buffer
                -> CULLong
                -- ^ Output hash length
                -> Ptr CChar
                -- ^ Constant message buffer
                -> CULLong
                -- ^ Message buffer length
                -> Ptr CChar
                -- ^ Constant Key buffer
                -> CULLong
                -- ^ Key buffer length
                -> IO CInt
                -- ^ Always 0