{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Crypto.Random
(
ChaChaDRG
, SystemDRG
, Seed
, seedNew
, seedFromInteger
, seedToInteger
, seedFromBinary
, getSystemDRG
, drgNew
, drgNewSeed
, drgNewTest
, withDRG
, withRandomBytes
, DRG(..)
, MonadRandom(..)
, MonadPseudoRandom
) where
import Crypto.Error
import Crypto.Random.Types
import Crypto.Random.ChaChaDRG
import Crypto.Random.SystemDRG
import Data.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
import qualified Data.ByteArray as B
import Crypto.Internal.Imports
import qualified Crypto.Number.Serialize as Serialize
newtype Seed = Seed ScrubbedBytes
deriving (ByteArrayAccess)
seedLength :: Int
seedLength = 40
seedNew :: MonadRandom randomly => randomly Seed
seedNew = Seed `fmap` getRandomBytes seedLength
seedToInteger :: Seed -> Integer
seedToInteger (Seed b) = Serialize.os2ip b
seedFromInteger :: Integer -> Seed
seedFromInteger i = Seed $ Serialize.i2ospOf_ seedLength (i `mod` 2^(seedLength * 8))
seedFromBinary :: ByteArrayAccess b => b -> CryptoFailable Seed
seedFromBinary b
| B.length b /= 40 = CryptoFailed (CryptoError_SeedSizeInvalid)
| otherwise = CryptoPassed $ Seed $ B.convert b
drgNew :: MonadRandom randomly => randomly ChaChaDRG
drgNew = drgNewSeed `fmap` seedNew
drgNewSeed :: Seed -> ChaChaDRG
drgNewSeed (Seed seed) = initialize seed
drgNewTest :: (Word64, Word64, Word64, Word64, Word64) -> ChaChaDRG
drgNewTest = initializeWords
withRandomBytes :: (ByteArray ba, DRG g) => g -> Int -> (ba -> a) -> (a, g)
withRandomBytes rng len f = (f bs, rng')
where (bs, rng') = randomBytesGenerate len rng