module Crypto.Saltine.Core.Hash (
ShorthashKey,
hash,
shorthash, newShorthashKey,
GenerichashKey,
newGenerichashKey,
GenerichashOutLen,
generichashOutLen, generichash
) where
import Crypto.Saltine.Internal.Hash
( c_hash
, c_generichash
, shorthash
, ShorthashKey(..)
, GenerichashKey(..)
, GenerichashOutLen(..)
)
import Crypto.Saltine.Internal.Util as U
import Data.ByteString (ByteString)
import qualified Crypto.Saltine.Internal.Hash as Bytes
import qualified Data.ByteString as S
hash :: ByteString
-> ByteString
hash :: ByteString -> ByteString
hash 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
Bytes.hash_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
m] (([CStringLen] -> IO CInt) -> IO CInt)
-> ([CStringLen] -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \[(Ptr CChar
pm, Int
_)] -> Ptr CChar -> Ptr CChar -> CULLong -> IO CInt
c_hash 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)
newShorthashKey :: IO ShorthashKey
newShorthashKey :: IO ShorthashKey
newShorthashKey = ByteString -> ShorthashKey
ShK (ByteString -> ShorthashKey) -> IO ByteString -> IO ShorthashKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO ByteString
randomByteString Int
Bytes.shorthash_keybytes
newGenerichashKey :: Int -> IO (Maybe GenerichashKey)
newGenerichashKey :: Int -> IO (Maybe GenerichashKey)
newGenerichashKey Int
n = if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
Bytes.generichash_keybytes_max
then GenerichashKey -> Maybe GenerichashKey
forall a. a -> Maybe a
Just (GenerichashKey -> Maybe GenerichashKey)
-> (ByteString -> GenerichashKey)
-> ByteString
-> Maybe GenerichashKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> GenerichashKey
GhK (ByteString -> Maybe GenerichashKey)
-> IO ByteString -> IO (Maybe GenerichashKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO ByteString
randomByteString Int
n
else Maybe GenerichashKey -> IO (Maybe GenerichashKey)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe GenerichashKey
forall a. Maybe a
Nothing
generichashOutLen :: Int -> Maybe GenerichashOutLen
generichashOutLen :: Int -> Maybe GenerichashOutLen
generichashOutLen Int
n = if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
Bytes.generichash_bytes_max
then GenerichashOutLen -> Maybe GenerichashOutLen
forall a. a -> Maybe a
Just (GenerichashOutLen -> Maybe GenerichashOutLen)
-> GenerichashOutLen -> Maybe GenerichashOutLen
forall a b. (a -> b) -> a -> b
$ Int -> GenerichashOutLen
GhOL (Int -> GenerichashOutLen) -> Int -> GenerichashOutLen
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
else Maybe GenerichashOutLen
forall a. Maybe a
Nothing
generichash :: GenerichashKey
-> ByteString
-> GenerichashOutLen
-> ByteString
generichash :: GenerichashKey -> ByteString -> GenerichashOutLen -> ByteString
generichash (GhK ByteString
k) ByteString
m (GhOL Int
outLen) = (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
outLen ((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
-> CULLong
-> Ptr CChar
-> CULLong
-> Ptr CChar
-> CULLong
-> IO CInt
c_generichash Ptr CChar
ph (Int -> CULLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
outLen) 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 (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
k)