{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Crypto.Random
(
EntropyPool
, createEntropyPool
, grabEntropy
, grabEntropyIO
, CPRG(..)
, withRandomBytes
, SystemRNG
, createTestEntropyPool
) where
import Crypto.Random.Entropy
import Crypto.Random.Generator
import Data.ByteString (ByteString)
import Data.Typeable (Typeable)
import qualified Data.ByteString.Internal as B (unsafeCreate)
data SystemRNG = SystemRNG EntropyPool
deriving Typeable
instance CPRG SystemRNG where
cprgCreate :: EntropyPool -> SystemRNG
cprgCreate EntropyPool
entPool = EntropyPool -> SystemRNG
SystemRNG EntropyPool
entPool
cprgSetReseedThreshold :: Int -> SystemRNG -> SystemRNG
cprgSetReseedThreshold Int
_ SystemRNG
r = SystemRNG
r
cprgFork :: SystemRNG -> (SystemRNG, SystemRNG)
cprgFork r :: SystemRNG
r@(SystemRNG EntropyPool
entPool) = (SystemRNG
r, EntropyPool -> SystemRNG
forall gen. CPRG gen => EntropyPool -> gen
cprgCreate EntropyPool
entPool)
cprgGenerate :: Int -> SystemRNG -> (ByteString, SystemRNG)
cprgGenerate Int
n g :: SystemRNG
g@(SystemRNG EntropyPool
entPool) = (Int -> (Ptr Word8 -> IO ()) -> ByteString
B.unsafeCreate Int
n (Int -> EntropyPool -> Ptr Word8 -> IO ()
grabEntropyPtr Int
n EntropyPool
entPool), SystemRNG
g)
cprgGenerateWithEntropy :: Int -> SystemRNG -> (ByteString, SystemRNG)
cprgGenerateWithEntropy Int
n SystemRNG
g = Int -> SystemRNG -> (ByteString, SystemRNG)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenerate Int
n SystemRNG
g
withRandomBytes :: CPRG g => g -> Int -> (ByteString -> a) -> (a, g)
withRandomBytes :: g -> Int -> (ByteString -> a) -> (a, g)
withRandomBytes g
rng Int
len ByteString -> a
f = (ByteString -> a
f ByteString
bs, g
rng')
where (ByteString
bs, g
rng') = Int -> g -> (ByteString, g)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenerate Int
len g
rng