crypton-1.0.1: Cryptography Primitives sink

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | stable
Portability | good
Safe Haskell | None
Language | Haskell2010

Crypto.Random

Contents

- Deterministic instances
- Seed
- Deterministic Random class
- Random abstraction

Description

Synopsis

- data ChaChaDRG
- data SystemDRG
- data Seed
- seedNew :: MonadRandom randomly => randomly Seed
- seedFromInteger :: Integer -> Seed
- seedToInteger :: Seed -> Integer
- seedFromBinary :: ByteArrayAccess b => b -> CryptoFailable Seed
- getSystemDRG :: IO SystemDRG
- drgNew :: MonadRandom randomly => randomly ChaChaDRG
- drgNewSeed :: Seed -> ChaChaDRG
- drgNewTest :: (Word64, Word64, Word64, Word64, Word64) -> ChaChaDRG
- withDRG :: DRG gen => gen -> MonadPseudoRandom gen a -> (a, gen)
- withRandomBytes :: (ByteArray ba, DRG g) => g -> Int -> (ba -> a) -> (a, g)
- class DRG gen where
  - randomBytesGenerate :: ByteArray byteArray => Int -> gen -> (byteArray, gen)
- class Monad m => MonadRandom (m :: Type -> Type) where
  - getRandomBytes :: ByteArray byteArray => Int -> m byteArray
- data MonadPseudoRandom gen a

# Deterministic instances

data ChaChaDRG #

ChaCha Deterministic Random Generator

#### Instances

Instances details

DRG ChaChaDRG # |
Instance details Defined in Crypto.Random.ChaChaDRG Methods randomBytesGenerate :: ByteArray byteArray => Int -> ChaChaDRG -> (byteArray, ChaChaDRG) #
NFData ChaChaDRG # |
Instance details Defined in Crypto.Random.ChaChaDRG Methods rnf :: ChaChaDRG -> () #

data SystemDRG #

A referentially transparent System representation of the random evaluated out of the system.

Holding onto a specific DRG means that all the already evaluated bytes will be consistently replayed.

There's no need to reseed this DRG, as only pure entropy is represented here.

#### Instances

Instances details

DRG SystemDRG # |
Instance details Defined in Crypto.Random.SystemDRG Methods randomBytesGenerate :: ByteArray byteArray => Int -> SystemDRG -> (byteArray, SystemDRG) #

data Seed #

#### Instances

Instances details

ByteArrayAccess Seed # |
Instance details Defined in Crypto.Random Methods length :: Seed -> Int # withByteArray :: Seed -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: Seed -> Ptr p -> IO () #

# Seed

seedNew :: MonadRandom randomly => randomly Seed #

Create a new Seed from system entropy

seedFromInteger :: Integer -> Seed #

Convert an integer to a Seed

seedToInteger :: Seed -> Integer #

Convert a Seed to an integer

seedFromBinary :: ByteArrayAccess b => b -> CryptoFailable Seed #

Convert a binary to a seed

# Deterministic Random class

getSystemDRG :: IO SystemDRG #

Grab one instance of the System DRG

drgNew :: MonadRandom randomly => randomly ChaChaDRG #

Create a new DRG from system entropy

drgNewSeed :: Seed -> ChaChaDRG #

Create a new DRG from a seed

drgNewTest :: (Word64, Word64, Word64, Word64, Word64) -> ChaChaDRG #

Create a new DRG from 5 Word64.

This is a convenient interface to create deterministic interface for quickcheck style testing.

It can also be used in other contexts provided the input has been properly randomly generated.

Note that the `Arbitrary` instance provided by QuickCheck for `Word64` does not have a uniform distribution. It is often better to use instead `arbitraryBoundedRandom`.

System endianness impacts how the tuple is interpreted and therefore changes the resulting DRG.

withDRG :: DRG gen => gen -> MonadPseudoRandom gen a -> (a, gen) #

Run a pure computation with a Deterministic Random Generator in the `MonadPseudoRandom`

withRandomBytes :: (ByteArray ba, DRG g) => g -> Int -> (ba -> a) -> (a, g) #

Generate `len random bytes and mapped the bytes to the function` f.

This is equivalent to use Control.Arrow `first` with `randomBytesGenerate`

class DRG gen where #

A Deterministic Random Generator (DRG) class

Methods

randomBytesGenerate :: ByteArray byteArray => Int -> gen -> (byteArray, gen) #

Generate N bytes of randomness from a DRG

#### Instances

Instances details

DRG ChaChaDRG # |
Instance details Defined in Crypto.Random.ChaChaDRG Methods randomBytesGenerate :: ByteArray byteArray => Int -> ChaChaDRG -> (byteArray, ChaChaDRG) #
DRG SystemDRG # |
Instance details Defined in Crypto.Random.SystemDRG Methods randomBytesGenerate :: ByteArray byteArray => Int -> SystemDRG -> (byteArray, SystemDRG) #

# Random abstraction

class Monad m => MonadRandom (m :: Type -> Type) where #

A monad constraint that allows to generate random bytes

Methods

getRandomBytes :: ByteArray byteArray => Int -> m byteArray #

#### Instances

Instances details

MonadRandom IO # |
Instance details Defined in Crypto.Random.Types Methods getRandomBytes :: ByteArray byteArray => Int -> IO byteArray #
DRG gen => MonadRandom (MonadPseudoRandom gen) # |
Instance details Defined in Crypto.Random.Types Methods getRandomBytes :: ByteArray byteArray => Int -> MonadPseudoRandom gen byteArray #

data MonadPseudoRandom gen a #

A simple Monad class very similar to a State Monad with the state being a DRG.

#### Instances

Instances details

DRG gen => MonadRandom (MonadPseudoRandom gen) # |
Instance details Defined in Crypto.Random.Types Methods getRandomBytes :: ByteArray byteArray => Int -> MonadPseudoRandom gen byteArray #
DRG gen => Applicative (MonadPseudoRandom gen) # |
Instance details Defined in Crypto.Random.Types Methods pure :: a -> MonadPseudoRandom gen a # (<*>) :: MonadPseudoRandom gen (a -> b) -> MonadPseudoRandom gen a -> MonadPseudoRandom gen b # liftA2 :: (a -> b -> c) -> MonadPseudoRandom gen a -> MonadPseudoRandom gen b -> MonadPseudoRandom gen c # (*>) :: MonadPseudoRandom gen a -> MonadPseudoRandom gen b -> MonadPseudoRandom gen b # (<*) :: MonadPseudoRandom gen a -> MonadPseudoRandom gen b -> MonadPseudoRandom gen a #
DRG gen => Functor (MonadPseudoRandom gen) # |
Instance details Defined in Crypto.Random.Types Methods fmap :: (a -> b) -> MonadPseudoRandom gen a -> MonadPseudoRandom gen b # (<$) :: a -> MonadPseudoRandom gen b -> MonadPseudoRandom gen a #
DRG gen => Monad (MonadPseudoRandom gen) # |
Instance details Defined in Crypto.Random.Types Methods (>>=) :: MonadPseudoRandom gen a -> (a -> MonadPseudoRandom gen b) -> MonadPseudoRandom gen b # (>>) :: MonadPseudoRandom gen a -> MonadPseudoRandom gen b -> MonadPseudoRandom gen b # return :: a -> MonadPseudoRandom gen a #