Copyright | (c) Leo D 2023 |
---|---|
License | BSD-3-Clause |
Maintainer | leo@apotheca.io |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Pseudo-random number generation.
Synopsis
- newtype RNG = MkRNG {
- getRNGForeignPtr :: ForeignPtr BotanRNGStruct
- type RNGType = ByteString
- withRNG :: RNG -> (BotanRNG -> IO a) -> IO a
- rngInit :: RNGType -> IO RNG
- rngDestroy :: RNG -> IO ()
- rngGet :: RNG -> Int -> IO ByteString
- systemRNGGet :: Int -> IO ByteString
- rngReseed :: RNG -> Int -> IO ()
- rngReseedFromRNG :: RNG -> RNG -> Int -> IO ()
- rngAddEntropy :: RNG -> ByteString -> IO ()
- pattern SystemRNG :: RNGType
- pattern UserRNG :: RNGType
- pattern UserThreadsafeRNG :: RNGType
- pattern RDRandRNG :: RNGType
Random number generators
A `random number generator` is used to generate uniform samples of pseudorandom bytes.
Usage
You can always use the system RNG
:
import Botan.Low.RNG randomBytes <- systemRNGGet 16
Unless you need a specific RNG
, it is strongly recommended that you use the
autoseeded user
RNG.
import Botan.Low.RNG rng <- rngInit "user" randomBytes <- rngGet rng 16
You can reseed a generator using the system generator:
rngReseed rng 64
You can also reseed a generator using a specific generator:
systemRNG <- rngInit "system" rngReseedFromRNG rng systemRNG 64
You can also seed it with your own entropy; this is safe and can never *decrease* the amount of entropy in the generator.
rngAddEntropy rng "Fee fi fo fum!"
MkRNG | |
|
type RNGType = ByteString Source #
Initialize a random number generator object
rng_type has the possible values:
- "system": system RNG
- "user": userspace RNG
- "user-threadsafe": userspace RNG, with internal locking
- "rdrand": directly read RDRAND
Set rng_type to null to let the library choose some default.
rngDestroy :: RNG -> IO () Source #
Destroy a random number generator object immediately
:: RNG | rng: rng object |
-> Int | out_len: number of requested bytes |
-> IO ByteString | out: output buffer of size out_len |
Get random bytes from a random number generator
:: Int | out_len: number of requested bytes |
-> IO ByteString | out: output buffer of size out_len |
Get random bytes from system random number generator
Reseed a random number generator
Uses the System_RNG as a seed generator.
:: RNG | rng: rng object |
-> RNG | source_rng: the rng that will be read from |
-> Int | bits: number of bits to reseed with |
-> IO () |
Reseed a random number generator
:: RNG | rng: rng object |
-> ByteString | entropy: the data to add |
-> IO () |
Add some seed material to a random number generator
RNG Types
pattern UserThreadsafeRNG :: RNGType Source #