{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Network.TLS.RNG
    ( StateRNG(..)
    , Seed
    , seedNew
    , seedToInteger
    , seedFromInteger
    , withTLSRNG
    , newStateRNG
    , MonadRandom
    , getRandomBytes
    ) where

import Crypto.Random.Types
import Crypto.Random

newtype StateRNG = StateRNG ChaChaDRG
    deriving (forall byteArray.
ByteArray byteArray =>
Int -> StateRNG -> (byteArray, StateRNG)
forall gen.
(forall byteArray.
 ByteArray byteArray =>
 Int -> gen -> (byteArray, gen))
-> DRG gen
randomBytesGenerate :: forall byteArray.
ByteArray byteArray =>
Int -> StateRNG -> (byteArray, StateRNG)
$crandomBytesGenerate :: forall byteArray.
ByteArray byteArray =>
Int -> StateRNG -> (byteArray, StateRNG)
DRG)

instance Show StateRNG where
    show :: StateRNG -> String
show StateRNG
_ = String
"rng[..]"

withTLSRNG :: StateRNG
           -> MonadPseudoRandom StateRNG a
           -> (a, StateRNG)
withTLSRNG :: forall a. StateRNG -> MonadPseudoRandom StateRNG a -> (a, StateRNG)
withTLSRNG StateRNG
rng MonadPseudoRandom StateRNG a
f = forall gen a. DRG gen => gen -> MonadPseudoRandom gen a -> (a, gen)
withDRG StateRNG
rng MonadPseudoRandom StateRNG a
f

newStateRNG :: Seed -> StateRNG
newStateRNG :: Seed -> StateRNG
newStateRNG Seed
seed = ChaChaDRG -> StateRNG
StateRNG forall a b. (a -> b) -> a -> b
$ Seed -> ChaChaDRG
drgNewSeed Seed
seed