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 n :: Int
n cprg :: 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 n :: Int
n cprg :: 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' len :: Int
len rng :: g
rng
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 = [Char] -> ([ByteString], g)
forall a. HasCallStack => [Char] -> a
error "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 g :: b
g n :: Int
n
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = ([], b
g)
| Bool
otherwise = let itBytes :: Int
itBytes = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (2Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^(20:: Int)) Int
n
(bs :: ByteString
bs, g' :: b
g') = Int -> b -> (ByteString, b)
forall gen. CPRG gen => Int -> gen -> (ByteString, gen)
cprgGenBytes Int
itBytes b
g
(l :: [ByteString]
l, g'' :: 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'')