| Maintainer | Thomas.DuBuisson@gmail.com |
|---|---|
| Stability | beta |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell98 |
Control.Monad.CryptoRandom
Description
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)
- crandoms :: CryptoRandomGen g => g -> [a]
- class CRandomR a where
- crandomR :: CryptoRandomGen g => (a, a) -> g -> Either GenError (a, g)
- crandomRs :: CryptoRandomGen g => (a, a) -> g -> [a]
- class (ContainsGenError e, MonadError e m) => MonadCRandom e m where
- getCRandom :: CRandom a => m a
- getBytes :: Int -> m ByteString
- getBytesWithEntropy :: Int -> ByteString -> m ByteString
- doReseed :: ByteString -> m ()
- class (ContainsGenError e, MonadError e m) => MonadCRandomR e m where
- getCRandomR :: CRandomR a => (a, a) -> m a
- class ContainsGenError e where
- toGenError :: e -> Maybe GenError
- fromGenError :: GenError -> e
- newtype CRandT g e m a = CRandT {}
- type CRand g e = CRandT g e Identity
- runCRandT :: ContainsGenError e => CRandT g e m a -> g -> m (Either e (a, g))
- evalCRandT :: (ContainsGenError e, Monad m) => CRandT g e m a -> g -> m (Either e a)
- runCRand :: ContainsGenError e => CRand g e a -> g -> Either e (a, g)
- evalCRand :: CRand g GenError a -> g -> Either GenError a
- newGenCRand :: (CryptoRandomGen g, MonadCRandom GenError m, Functor m) => m g
- liftCRand :: (g -> Either e (a, g)) -> CRand g e a
- liftCRandT :: Monad m => (g -> Either e (a, g)) -> CRandT g e m a
- module Crypto.Random
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).
Minimal complete definition
Methods
crandom :: CryptoRandomGen g => g -> Either GenError (a, g) Source
crandoms :: CryptoRandomGen g => g -> [a] Source
Minimal complete definition
Methods
crandomR :: CryptoRandomGen g => (a, a) -> g -> Either GenError (a, g) Source
crandomRs :: CryptoRandomGen g => (a, a) -> g -> [a] Source
class (ContainsGenError e, MonadError e m) => MonadCRandom e m where Source
MonadCRandom m represents a monad that can produce
random values (or fail with a GenError). It is suggested
you use the CRandT transformer in your monad stack.
Methods
getCRandom :: CRandom a => m a Source
getBytes :: Int -> m ByteString Source
getBytesWithEntropy :: Int -> ByteString -> m ByteString Source
doReseed :: ByteString -> m () Source
Instances
| MonadCRandom e m => MonadCRandom e (ReaderT r m) Source | |
| (Monoid w, MonadCRandom e m) => MonadCRandom e (WriterT w m) Source | |
| (Monoid w, MonadCRandom e m) => MonadCRandom e (WriterT w m) Source | |
| MonadCRandom e m => MonadCRandom e (StateT s m) Source | |
| MonadCRandom e m => MonadCRandom e (StateT s m) Source | |
| (ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandom e (CRandT g e m) Source | |
| (Monoid w, MonadCRandom e m) => MonadCRandom e (RWST r w s m) Source | |
| (Monoid w, MonadCRandom e m) => MonadCRandom e (RWST r w s m) Source |
class (ContainsGenError e, MonadError e m) => MonadCRandomR e m where Source
Methods
getCRandomR :: CRandomR a => (a, a) -> m a Source
Instances
| MonadCRandomR e m => MonadCRandomR e (ReaderT r m) Source | |
| (MonadCRandomR e m, Monoid w) => MonadCRandomR e (WriterT w m) Source | |
| (MonadCRandomR e m, Monoid w) => MonadCRandomR e (WriterT w m) Source | |
| MonadCRandomR e m => MonadCRandomR e (StateT s m) Source | |
| MonadCRandomR e m => MonadCRandomR e (StateT s m) Source | |
| (ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandomR e (CRandT g e m) Source | |
| (MonadCRandomR e m, Monoid w) => MonadCRandomR e (RWST r w s m) Source | |
| (MonadCRandomR e m, Monoid w) => MonadCRandomR e (RWST r w s m) Source |
class ContainsGenError e where Source
Instances
CRandT is the transformer suggested for MonadCRandom.
Instances
| Monad m => MonadError e (CRandT g e m) Source | |
| MonadReader r m => MonadReader r (CRandT g e m) Source | |
| MonadState s m => MonadState s (CRandT g e m) Source | |
| MonadWriter w m => MonadWriter w (CRandT g e m) Source | |
| (ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandomR e (CRandT g e m) Source | |
| (ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandom e (CRandT g e m) Source | |
| MonadTrans (CRandT g e) Source | |
| Monad m => Monad (CRandT g e m) Source | |
| Functor m => Functor (CRandT g e m) Source | |
| MonadFix m => MonadFix (CRandT g e m) Source | |
| Monad m => Applicative (CRandT g e m) Source | |
| MonadIO m => MonadIO (CRandT g e m) Source | |
| MonadCont m => MonadCont (CRandT g e m) Source |
type CRand g e = CRandT g e Identity Source
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 @
runCRandT :: ContainsGenError e => CRandT g e m a -> g -> m (Either e (a, g)) Source
evalCRandT :: (ContainsGenError e, Monad m) => CRandT g e m a -> g -> m (Either e a) Source
runCRand :: ContainsGenError e => CRand g e a -> g -> Either e (a, g) Source
newGenCRand :: (CryptoRandomGen g, MonadCRandom GenError m, Functor m) => m g Source
liftCRandT :: Monad m => (g -> Either e (a, g)) -> CRandT g e m a Source
module Crypto.Random