Portability | portable |
---|---|
Stability | beta |
Maintainer | Thomas.DuBuisson@gmail.com |
Much like the MonadRandom package (Control.Monad.Random), this module provides plumbing for the CryptoRandomGen generators.
- class CRandom a where
- crandom :: CryptoRandomGen g => g -> Either GenError (a, g)
- crandomR :: CryptoRandomGen g => (a, a) -> g -> Either GenError (a, g)
- crandoms :: CryptoRandomGen g => g -> [a]
- crandomRs :: CryptoRandomGen g => (a, a) -> g -> [a]
- class MonadError GenError m => MonadCryptoRandom m where
- getCRandom :: CRandom a => m a
- getCRandomR :: CRandom a => (a, a) -> m a
- getBytes :: Int -> m ByteString
- getBytesWithEntropy :: Int -> ByteString -> m ByteString
- doReseed :: ByteString -> m ()
- data CRandT g m a
- type CRand g = CRandT g Identity
- runCRandT :: CRandT g m a -> g -> m (Either GenError (a, g))
- evalCRandT :: Monad m => CRandT g m a -> g -> m (Either GenError a)
- runCRand :: CRand g a -> g -> Either GenError (a, g)
- evalCRand :: CRand g a -> g -> Either GenError a
Documentation
CRandom a
is much like the Random
class from the System.Random module in the random package.
The main difference is CRandom builds on crypto-api's CryptoRandomGen
, so it allows
explicit failure.
crandomR (low,high) g
as typically instantiated will generate a value between
[low, high] inclusively, swapping the pair if high < low.
Provided instances for crandom g
generates randoms between the bounds and between +/- 2^256
for Integer.
The crandomR
function has degraded (theoretically unbounded, probabilistically decent) performance
the closer your range size (high - low) is to 2^n (from the top).
crandom :: CryptoRandomGen g => g -> Either GenError (a, g)Source
crandomR :: CryptoRandomGen g => (a, a) -> g -> Either GenError (a, g)Source
crandoms :: CryptoRandomGen g => g -> [a]Source
crandomRs :: CryptoRandomGen g => (a, a) -> g -> [a]Source
class MonadError GenError m => MonadCryptoRandom m whereSource
MonadCryptoRandom m
represents a monad that can produce
random values (or fail with a GenError
). It is suggestd
you use the CRandT
transformer in your monad stack.
getCRandom :: CRandom a => m aSource
getCRandomR :: CRandom a => (a, a) -> m aSource
getBytes :: Int -> m ByteStringSource
getBytesWithEntropy :: Int -> ByteString -> m ByteStringSource
doReseed :: ByteString -> m ()Source
(Monad m, CryptoRandomGen g) => MonadCryptoRandom (CRandT g m) |
CRandT is the transformer suggested for MonadCryptoRandom.
Monad m => MonadError GenError (CRandT g m) | |
MonadTrans (CRandT g) | |
Monad m => Monad (CRandT g m) | |
(Monad m, CryptoRandomGen g) => MonadCryptoRandom (CRandT g m) |
type CRand g = CRandT g IdentitySource
Simple users of generators can use CRand for
quick and easy generation of randoms. See
below for a simple use of newGenIO
(from crypto-api),
getCRandom
, getBytes
, and runCRandom
.
getRandPair = do int <- getCRandom bytes <- getBytes 100 return (int, bytes) func = do g <- newGenIO case runCRand getRandPair g of Right ((int,bytes), g') -> useRandomVals (int,bytes) Left x -> handleGenError x