{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, DeriveGeneric, ForeignFunctionInterface #-}
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
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
$ccompare :: ShorthashKey -> ShorthashKey -> Ordering
compare :: ShorthashKey -> ShorthashKey -> Ordering
$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
>= :: ShorthashKey -> ShorthashKey -> Bool
$cmax :: ShorthashKey -> ShorthashKey -> ShorthashKey
max :: ShorthashKey -> ShorthashKey -> ShorthashKey
$cmin :: ShorthashKey -> ShorthashKey -> ShorthashKey
min :: ShorthashKey -> ShorthashKey -> ShorthashKey
Ord, Eq ShorthashKey
Eq ShorthashKey
-> (Int -> ShorthashKey -> Int)
-> (ShorthashKey -> Int)
-> Hashable ShorthashKey
Int -> ShorthashKey -> Int
ShorthashKey -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> ShorthashKey -> Int
hashWithSalt :: Int -> ShorthashKey -> Int
$chash :: ShorthashKey -> Int
hash :: ShorthashKey -> Int
Hashable, Typeable ShorthashKey
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 -> Constr
ShorthashKey -> DataType
(forall b. Data b => b -> b) -> ShorthashKey -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ShorthashKey -> c ShorthashKey
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ShorthashKey
$ctoConstr :: ShorthashKey -> Constr
toConstr :: ShorthashKey -> Constr
$cdataTypeOf :: ShorthashKey -> DataType
dataTypeOf :: ShorthashKey -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ShorthashKey)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ShorthashKey)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ShorthashKey)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ShorthashKey)
$cgmapT :: (forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey
gmapT :: (forall b. Data b => b -> b) -> ShorthashKey -> ShorthashKey
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ShorthashKey -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ShorthashKey -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> ShorthashKey -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ShorthashKey -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m ShorthashKey
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ShorthashKey -> m 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
$cfrom :: forall x. ShorthashKey -> Rep ShorthashKey x
from :: forall x. ShorthashKey -> Rep ShorthashKey x
$cto :: forall x. Rep ShorthashKey x -> ShorthashKey
to :: forall x. Rep ShorthashKey x -> ShorthashKey
Generic, ShorthashKey -> ()
(ShorthashKey -> ()) -> NFData ShorthashKey
forall a. (a -> ()) -> NFData a
$crnf :: ShorthashKey -> ()
rnf :: 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
"\"}"
nullShKey :: ShorthashKey
nullShKey :: ShorthashKey
nullShKey = ByteString -> ShorthashKey
ShK (Int -> Char -> ByteString
S8.replicate Int
shorthash_keybytes Char
'\NUL')
shorthash :: ShorthashKey
-> ByteString
-> ByteString
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 #-}
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
$ccompare :: GenerichashKey -> GenerichashKey -> Ordering
compare :: GenerichashKey -> GenerichashKey -> Ordering
$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
>= :: GenerichashKey -> GenerichashKey -> Bool
$cmax :: GenerichashKey -> GenerichashKey -> GenerichashKey
max :: GenerichashKey -> GenerichashKey -> GenerichashKey
$cmin :: GenerichashKey -> GenerichashKey -> GenerichashKey
min :: GenerichashKey -> GenerichashKey -> GenerichashKey
Ord, Eq GenerichashKey
Eq GenerichashKey
-> (Int -> GenerichashKey -> Int)
-> (GenerichashKey -> Int)
-> Hashable GenerichashKey
Int -> GenerichashKey -> Int
GenerichashKey -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> GenerichashKey -> Int
hashWithSalt :: Int -> GenerichashKey -> Int
$chash :: GenerichashKey -> Int
hash :: GenerichashKey -> Int
Hashable, Typeable GenerichashKey
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 -> Constr
GenerichashKey -> DataType
(forall b. Data b => b -> b) -> GenerichashKey -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashKey -> c GenerichashKey
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashKey
$ctoConstr :: GenerichashKey -> Constr
toConstr :: GenerichashKey -> Constr
$cdataTypeOf :: GenerichashKey -> DataType
dataTypeOf :: GenerichashKey -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashKey)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashKey)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashKey)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashKey)
$cgmapT :: (forall b. Data b => b -> b) -> GenerichashKey -> GenerichashKey
gmapT :: (forall b. Data b => b -> b) -> GenerichashKey -> GenerichashKey
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashKey -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashKey -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashKey -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashKey -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m GenerichashKey
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashKey -> m 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
$cfrom :: forall x. GenerichashKey -> Rep GenerichashKey x
from :: forall x. GenerichashKey -> Rep GenerichashKey x
$cto :: forall x. Rep GenerichashKey x -> GenerichashKey
to :: forall x. Rep GenerichashKey x -> GenerichashKey
Generic, GenerichashKey -> ()
(GenerichashKey -> ()) -> NFData GenerichashKey
forall a. (a -> ()) -> NFData a
$crnf :: GenerichashKey -> ()
rnf :: 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
$c== :: GenerichashOutLen -> GenerichashOutLen -> Bool
== :: GenerichashOutLen -> GenerichashOutLen -> Bool
$c/= :: GenerichashOutLen -> GenerichashOutLen -> Bool
/= :: 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
$ccompare :: GenerichashOutLen -> GenerichashOutLen -> Ordering
compare :: GenerichashOutLen -> GenerichashOutLen -> Ordering
$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
>= :: GenerichashOutLen -> GenerichashOutLen -> Bool
$cmax :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
max :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
$cmin :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
min :: GenerichashOutLen -> GenerichashOutLen -> GenerichashOutLen
Ord, Eq GenerichashOutLen
Eq GenerichashOutLen
-> (Int -> GenerichashOutLen -> Int)
-> (GenerichashOutLen -> Int)
-> Hashable GenerichashOutLen
Int -> GenerichashOutLen -> Int
GenerichashOutLen -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> GenerichashOutLen -> Int
hashWithSalt :: Int -> GenerichashOutLen -> Int
$chash :: GenerichashOutLen -> Int
hash :: GenerichashOutLen -> Int
Hashable, Typeable GenerichashOutLen
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 -> Constr
GenerichashOutLen -> DataType
(forall b. Data b => b -> b)
-> GenerichashOutLen -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GenerichashOutLen -> c GenerichashOutLen
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c GenerichashOutLen
$ctoConstr :: GenerichashOutLen -> Constr
toConstr :: GenerichashOutLen -> Constr
$cdataTypeOf :: GenerichashOutLen -> DataType
dataTypeOf :: GenerichashOutLen -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c GenerichashOutLen)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashOutLen)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c GenerichashOutLen)
$cgmapT :: (forall b. Data b => b -> b)
-> GenerichashOutLen -> GenerichashOutLen
gmapT :: (forall b. Data b => b -> b)
-> GenerichashOutLen -> GenerichashOutLen
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GenerichashOutLen -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashOutLen -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> GenerichashOutLen -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> GenerichashOutLen -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m GenerichashOutLen
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GenerichashOutLen -> m 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
$cfrom :: forall x. GenerichashOutLen -> Rep GenerichashOutLen x
from :: forall x. GenerichashOutLen -> Rep GenerichashOutLen x
$cto :: forall x. Rep GenerichashOutLen x -> GenerichashOutLen
to :: forall x. Rep GenerichashOutLen x -> GenerichashOutLen
Generic, GenerichashOutLen -> ()
(GenerichashOutLen -> ()) -> NFData GenerichashOutLen
forall a. (a -> ()) -> NFData a
$crnf :: GenerichashOutLen -> ()
rnf :: GenerichashOutLen -> ()
NFData)
hash_bytes, shorthash_bytes, shorthash_keybytes, generichash_bytes_max, generichash_keybytes_max :: Int
hash_bytes :: Int
hash_bytes = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_hash_bytes
shorthash_bytes :: Int
shorthash_bytes = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_shorthash_bytes
shorthash_keybytes :: Int
shorthash_keybytes = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_shorthash_keybytes
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
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
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
foreign import ccall "crypto_hash_bytes"
c_crypto_hash_bytes :: CSize
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
-> Ptr CChar
-> CULLong
-> IO CInt
foreign import ccall "crypto_shorthash"
c_shorthash :: Ptr CChar
-> Ptr CChar
-> CULLong
-> Ptr CChar
-> IO CInt
foreign import ccall "crypto_generichash"
c_generichash :: Ptr CChar
-> CULLong
-> Ptr CChar
-> CULLong
-> Ptr CChar
-> CULLong
-> IO CInt