packages feed

crypto-random-effect (empty) → 0.1.0

raw patch · 5 files changed

+147/−0 lines, 5 filesdep +basedep +bytestringdep +crypto-randomsetup-changed

Dependencies added: base, bytestring, crypto-random, extensible-effects, securemem

Files

+ Changelog.md view
@@ -0,0 +1,5 @@+# Changelog++## Version 0.1++Targets `crypto-random` 0.0.7 and later.
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2014 Tobias Florek (me@ibotty.net)++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ crypto-random-effect.cabal view
@@ -0,0 +1,29 @@+-- Initial crypto-random-effect.cabal generated by cabal init.  For further+--  documentation, see http://haskell.org/cabal/users-guide/++name:                crypto-random-effect+version:             0.1.0+synopsis:            A random effect using crypto-random+-- description:         +homepage:            https://githu+license:             MIT+license-file:        LICENSE+author:              Tobias Florek+maintainer:          tob@butter.sh+-- copyright:           +category:            Effect, Crypto+build-type:          Simple+extra-source-files:  Changelog.md+cabal-version:       >=1.10++library+  exposed-modules:     Crypto.Random.Effect+  -- other-modules:       +  -- other-extensions:    +  build-depends:       base >=4.6 && <4.7+                     , bytestring <= 0.11+                     , crypto-random >=0.0.7 && <0.1+                     , extensible-effects ==1.2.*+                     , securemem ==0.1.*+  hs-source-dirs:      src+  default-language:    Haskell2010
+ src/Crypto/Random/Effect.hs view
@@ -0,0 +1,90 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TypeOperators #-}+-- | A Random effect.+--+-- Any ideas to let the user specify the random number generator ('C.CPRG')+-- instead of hardcoding 'C.SystemRNG' without complicating the api and+-- reinventing state as 'SetMember' is very welcome.+module Crypto.Random.Effect+  ( RNG()+  , runRNG+  , runRNGWithPool+  , withRNG+  , withRNGIO+  , rngFork+  , randomBytes+  , randomBytesWithEntropy+  , withRandomBytes+  , createEntropyPool+  , grabEntropy+  , unsafeGrabEntropy+  -- | reexports from 'crypto-random'+  , C.CPRG()+  , C.SystemRNG()+  , C.EntropyPool()+  ) where++import Control.Eff+import Control.Eff.Lift+import Control.Eff.State.Strict+import Control.Eff.Reader.Strict+import Data.ByteString (ByteString)+import Data.SecureMem (SecureMem)+import Data.Typeable (Typeable)+import qualified Crypto.Random as C++type RNG = State C.SystemRNG++deriving instance Typeable C.SystemRNG+deriving instance Typeable C.EntropyPool++-- | Run the effect.+runRNG+  :: SetMember Lift (Lift IO) r+  => Eff (RNG :> Reader C.EntropyPool :> r) a -> Eff r a+runRNG e = lift C.createEntropyPool >>= flip runRNGWithPool e++runRNGWithPool+  :: SetMember Lift (Lift IO) r+  => C.EntropyPool -> Eff (RNG :> Reader C.EntropyPool :> r) a -> Eff r a+runRNGWithPool pool = flip runReader pool . evalState (C.cprgCreate pool)++withRNG :: Member RNG r => (C.SystemRNG -> Eff r (a, C.SystemRNG)) -> Eff r a+withRNG f = do+    rng <- get+    (a, rng') <- f rng+    put rng'+    return a++withRNGPure :: Member RNG r => (C.SystemRNG -> (a, C.SystemRNG)) -> Eff r a+withRNGPure f = withRNG (return . f)++withRNGIO+  :: (SetMember Lift (Lift IO) r, Member RNG r)+  => (C.SystemRNG -> IO (a, C.SystemRNG)) -> Eff r a+withRNGIO f = withRNG (lift . f)++rngFork :: Member RNG r => Eff r C.SystemRNG+rngFork = withRNGPure C.cprgFork++randomBytes :: Member RNG r => Int -> Eff r ByteString+randomBytes = withRNGPure . C.cprgGenerate++randomBytesWithEntropy :: Member RNG r => Int -> Eff r ByteString+randomBytesWithEntropy = withRNGPure . C.cprgGenerateWithEntropy++withRandomBytes :: Member RNG r => Int -> (ByteString -> Eff r a) -> Eff r a+withRandomBytes cnt f = randomBytes cnt >>= f++createEntropyPool :: SetMember Lift (Lift IO) r => Eff r C.EntropyPool+createEntropyPool = lift C.createEntropyPool++grabEntropy+  :: (SetMember Lift (Lift IO) r, Member (Reader C.EntropyPool) r)+  => Int -> Eff r SecureMem+grabEntropy cnt = ask >>= lift . C.grabEntropyIO cnt++unsafeGrabEntropy :: Member (Reader C.EntropyPool) r => Int -> Eff r SecureMem+unsafeGrabEntropy cnt = fmap (C.grabEntropy cnt) ask