git.iko.soy/typeable/entropy
on main help sign in
Updated readme
f693ea7Thomas M. DuBuisson
README.md+27−7
@@ -5,17 +5,37 @@ security applications by calling out to either windows crypto api, unix/linux's
5`/dev/urandom`. Hardware RNGs (currently RDRAND, patches welcome) are supported
5`/dev/urandom`. Hardware RNGs (currently RDRAND, patches welcome) are supported
6via the `hardwareRNG` function.
6via the `hardwareRNG` function.
7
7
8−If you wish to obtain an XOR of the hardware and system RNG consider:
8+## Quick Start
9+
10+To simply get random bytes use `getEntropy`:
11+
12+```
13+#!/usr/bin/env cabal
14+{- cabal:
15+ build-depends: base, entropy, bytestring
16+-}
17+import qualified Data.ByteString as BS
18+import System.Entropy
19+
20+main :: IO ()
21+main = print . BS.unpack =<< getEntropy 16
22+-- Example output: [241,191,215,193,225,27,121,244,16,155,252,41,131,38,6,100]
23+```
24+
25+## Faster Randoms from Hardware
26+
27+Most x86 systems include a hardware random number generator. These can be
28+faster but require more trust in the platform:
9
29
10```
30```
11−import Data.Bits (xor)
12import qualified Data.ByteString as B
31import qualified Data.ByteString as B
13−import qualified Control.Exception as X
32+import System.Entropy
33+
34+eitherRNG :: Int -> IO B.ByteString
35+eitherRNG sz = maybe (getEntropy sz) pure =<< getHardwareEntropy sz
14
36
15−xorRNG sz = do hw <- hardwareRNG sz
37+main :: IO ()
16− h <- openHandle
38+main = print . B.unpack =<< eitherRNG 32
17− sys <- hGetEntropy h `X.finally` closeHandle h
18− pure $ B.pack $ B.zipWith xor hw sys
19```
39```
20
40
21This package supports Windows, {li,u}nix, QNX, and has preliminary support for HaLVM.
41This package supports Windows, {li,u}nix, QNX, and has preliminary support for HaLVM.