module Crypto.Random.Probabilistic (
probabilisticFrom,
) where
import Crypto.Hash (SHA512 (..), hashWith)
import Crypto.Internal.ByteArray (ByteArrayAccess, ScrubbedBytes)
import qualified Crypto.Internal.ByteArray as B
import Crypto.Internal.Compat
import Crypto.Random
import Crypto.Random.ChaChaDRG (initialize)
probabilisticFrom
:: ByteArrayAccess seed
=> seed
-> MonadPseudoRandom ChaChaDRG a
-> a
probabilisticFrom :: forall seed a.
ByteArrayAccess seed =>
seed -> MonadPseudoRandom ChaChaDRG a -> a
probabilisticFrom seed
material MonadPseudoRandom ChaChaDRG a
f = (a, ChaChaDRG) -> a
forall a b. (a, b) -> a
fst ((a, ChaChaDRG) -> a) -> (a, ChaChaDRG) -> a
forall a b. (a -> b) -> a -> b
$ ChaChaDRG -> MonadPseudoRandom ChaChaDRG a -> (a, ChaChaDRG)
forall gen a. DRG gen => gen -> MonadPseudoRandom gen a -> (a, gen)
withDRG ChaChaDRG
drg MonadPseudoRandom ChaChaDRG a
f
where
drg :: ChaChaDRG
drg = ScrubbedBytes -> ChaChaDRG
forall seed. ByteArrayAccess seed => seed -> ChaChaDRG
initialize (Int -> ScrubbedBytes -> ScrubbedBytes
forall bs. ByteArray bs => Int -> bs -> bs
B.take Int
seedLength (Digest SHA512 -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert Digest SHA512
digest :: ScrubbedBytes))
digest :: Digest SHA512
digest = SHA512 -> ScrubbedBytes -> Digest SHA512
forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA512
SHA512 (ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes
forall bs. ByteArray bs => bs -> bs -> bs
B.append ScrubbedBytes
secret (seed -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert seed
material) :: ScrubbedBytes)
seedLength :: Int
seedLength = Int
40
secret :: ScrubbedBytes
secret :: ScrubbedBytes
secret = IO ScrubbedBytes -> ScrubbedBytes
forall a. IO a -> a
unsafeDoIO (Int -> IO ScrubbedBytes
forall byteArray. ByteArray byteArray => Int -> IO byteArray
forall (m :: * -> *) byteArray.
(MonadRandom m, ByteArray byteArray) =>
Int -> m byteArray
getRandomBytes Int
32)
{-# NOINLINE secret #-}