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 :: Int -> g -> (ByteString, g)
cprgGenBytes Int
n g
cprg = Int -> g -> (ByteString, g)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenerate Int
n g
cprg
{-# DEPRECATED genRandomBytes "use cprgGenerate from Crypto.Random instead" #-}
genRandomBytes :: CPRG g
=> Int
-> g
-> (ByteString, g)
genRandomBytes :: Int -> g -> (ByteString, g)
genRandomBytes Int
n g
cprg = Int -> g -> (ByteString, g)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenerate Int
n g
cprg
genRandomBytes' :: CPRG g => Int
-> g
-> ([ByteString], g)
genRandomBytes' :: Int -> g -> ([ByteString], g)
genRandomBytes' Int
len g
rng
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = [Char] -> ([ByteString], g)
forall a. HasCallStack => [Char] -> a
error [Char]
"genBytes: cannot request negative amount of bytes."
| Bool
otherwise = g -> Int -> ([ByteString], g)
forall b. CPRG b => b -> Int -> ([ByteString], b)
loop g
rng Int
len
where loop :: b -> Int -> ([ByteString], b)
loop b
g Int
n
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = ([], b
g)
| Bool
otherwise = let itBytes :: Int
itBytes = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
2Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
20:: Int)) Int
n
(ByteString
bs, b
g') = Int -> b -> (ByteString, b)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenBytes Int
itBytes b
g
([ByteString]
l, b
g'') = Int -> b -> ([ByteString], b)
forall g. CPRG g => Int -> g -> ([ByteString], g)
genRandomBytes' (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
itBytes) b
g'
in (ByteString
bsByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
l, b
g'')