packages feed

monadcryptorandom 0.4.1 → 0.5

raw patch · 2 files changed

+37/−20 lines, 2 filesdep +taggeddep ~bytestring

Dependencies added: tagged

Dependency ranges changed: bytestring

Files

Control/Monad/CryptoRandom.hs view
@@ -1,4 +1,5 @@-{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, FlexibleContexts, GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, FlexibleContexts+  , GeneralizedNewtypeDeriving, MultiParamTypeClasses #-} {-|   Maintainer: Thomas.DuBuisson@gmail.com   Stability: beta@@ -11,17 +12,18 @@   module Control.Monad.CryptoRandom         ( CRandom(..)-	, CRandomR(..)+        , CRandomR(..)         , MonadCRandom(..)         , MonadCRandomR(..)---        , MonadCRand-	, ContainsGenError(..)+        , ContainsGenError(..)         , CRandT         , CRand         , runCRandT         , evalCRandT         , runCRand         , evalCRand+        , newGenCRand+        , module Crypto.Random         ) where  import Control.Applicative@@ -36,6 +38,7 @@ import Data.Int import Data.List (foldl') import Data.Word+import Data.Proxy import qualified Data.ByteString as B  -- |@MonadCRandom m@ represents a monad that can produce@@ -47,20 +50,29 @@         getBytesWithEntropy :: Int -> B.ByteString -> m B.ByteString         doReseed :: B.ByteString -> m () +newGenCRand :: (CryptoRandomGen g, MonadCRandom GenError m, Functor m) => m g+newGenCRand = go 0+  where+  go 1000 = throwError (GenErrorOther "The generator instance requested by newGenCRand never instantiates.")+  go i = do let p = Proxy+                getTypedGen :: (Functor m, CryptoRandomGen g, MonadCRandom GenError m)+                            => Proxy g -> m (Either GenError g)+                getTypedGen pr = fmap newGen (getBytes $ proxy genSeedLength pr)+            res <- getTypedGen p +            case res of+                Left _  -> go (i+1)+                Right g -> return (g `asProxyTypeOf` p)+ class (ContainsGenError e, MonadError e m) => MonadCRandomR e m where         getCRandomR  :: CRandomR a => (a,a) -> m a --- |A superclass including MonadCRandom and MonadCRandomR---class (MonadCRandom e m, MonadCRandomR e m) => MonadCRand e m---instance (MonadCRandom e m, MonadCRandomR e m) => MonadCRand e m- class ContainsGenError e where-	toGenError :: e -> Maybe GenError-	fromGenError :: GenError -> e+        toGenError :: e -> Maybe GenError+        fromGenError :: GenError -> e  instance ContainsGenError GenError where-	toGenError = Just-	fromGenError = id+        toGenError = Just+        fromGenError = id  -- |@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@@ -162,8 +174,8 @@         in case offset of         Left err -> Left err         Right (bs, g') ->-		let res = fromIntegral low + (bs2i bs .&. mask)-		in if res > fromIntegral high then go g' else Right (fromIntegral res, g')+                let res = fromIntegral low + (bs2i bs .&. mask)+                in if res > fromIntegral high then go g' else Right (fromIntegral res, g') {-# INLINE crandomR_Num #-}  wrap :: (Monad m, ContainsGenError e, Error e) => (g -> Either GenError (a,g)) -> CRandT g e m a@@ -181,7 +193,7 @@   (<*>) = ap  instance (Error e) => MonadTrans (CRandT g e) where-	lift = CRandT . lift . lift+        lift = CRandT . lift . lift  -- |Simple users of generators can use CRand for -- quick and easy generation of randoms.  See
monadcryptorandom.cabal view
@@ -1,5 +1,5 @@ name:           monadcryptorandom-version:        0.4.1+version:        0.5 license:        BSD3 license-file:   LICENSE copyright:      Thomas DuBuisson <thomas.dubuisson@gmail.com>@@ -8,8 +8,7 @@ description:    A monad for using CryptoRandomGen synopsis:       A monad for using CryptoRandomGen category:       Control, Cryptography-homepage:       http://trac.haskell.org/crypto-api/wiki-bug-reports:    http://trac.haskell.org/crypto-api/report/1+homepage:       https://github.com/TomMD/monadcryptorandom stability:      stable build-type:     Simple cabal-version:  >= 1.6@@ -19,8 +18,14 @@  Library   Build-Depends: base == 4.*,-                 bytestring >= 0.9 && < 0.10,-                 mtl >= 2.0, crypto-api >= 0.2, transformers >= 0.2+                 bytestring >= 0.9 && < 0.11,+                 mtl >= 2.0, crypto-api >= 0.2, transformers >= 0.2,+                 tagged >= 0.2   ghc-options:   hs-source-dirs:   exposed-modules: Control.Monad.CryptoRandom++source-repository head+  type:     git+  location: https://github.com/TomMD/monadcryptorandom+