-- |
-- Module      : Crypto.Random.API
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : Good
--
-- Deprecated interface for compatibility of crypto-random-api user
-- with crypto-random
--
module Crypto.Random.API
    ( CPRG(..)
    , cprgGenBytes
    , genRandomBytes
    , genRandomBytes'
    , withRandomBytes
    ) where

import Data.ByteString (ByteString)
import Crypto.Random

-- | Generate bytes using the CPRG and the number specified.
--
-- For user of the API, it's recommended to use genRandomBytes
-- instead of this method directly. the CPRG need to be able
-- to supply at minimum 2^20 bytes at a time.
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

-- | Generate bytes using the cprg in parameter.
--
-- If the number of bytes requested is really high,
-- it's preferable to use 'genRandomBytes' for better memory efficiency.
{-# DEPRECATED genRandomBytes "use cprgGenerate from Crypto.Random instead" #-}
genRandomBytes :: CPRG g    
               => Int -- ^ number of bytes to return
               -> g   -- ^ CPRG to use
               -> (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

-- | Generate bytes using the cprg in parameter.
--
-- This is not tail recursive and an excessive len (>= 2^29) parameter would
-- result in stack overflow.
genRandomBytes' :: CPRG g => Int -- ^ number of bytes to return
                          -> g   -- ^ CPRG to use
                          -> ([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'')