module Crypto.Random.API
( CPRG(..)
, cprgGenBytes
, genRandomBytes
, genRandomBytes'
, withRandomBytes
) where
import Data.ByteString (ByteString)
import Crypto.Random
cprgGenBytes :: CPRG g => Int -> g -> (ByteString, g)
cprgGenBytes n cprg = cprgGenerate n cprg
{-# DEPRECATED genRandomBytes "use cprgGenerate from Crypto.Random instead" #-}
genRandomBytes :: CPRG g
=> Int
-> g
-> (ByteString, g)
genRandomBytes n cprg = cprgGenerate n cprg
genRandomBytes' :: CPRG g => Int
-> g
-> ([ByteString], g)
genRandomBytes' len rng
| len < 0 = error "genBytes: cannot request negative amount of bytes."
| otherwise = loop rng len
where loop g n
| n == 0 = ([], g)
| otherwise = let itBytes = min (2^(20:: Int)) n
(bs, g') = cprgGenBytes itBytes g
(l, g'') = genRandomBytes' (n-itBytes) g'
in (bs:l, g'')