git.iko.soy/typeable/entropy
on c33dece help sign in
48 lines 1.2 kB history raw
1{-# LANGUAGE CPP, ForeignFunctionInterface, BangPatterns, ScopedTypeVariables #-}
2{-|
3 Maintainer: Thomas.DuBuisson@gmail.com
4 Stability: beta
5 Portability: portable
7 Obtain entropy from system sources or x86 RDRAND when available.
9 Currently supporting:
11 - Windows via CryptoAPI
12 - *nix systems via @\/dev\/urandom@
13 - Includes QNX
14 - Xen (only when RDRAND is available)
15-}
17module System.Entropy
18 ( getEntropy,
19#if defined(isWindows)
20 module System.EntropyWindows
21 ) where
22import System.EntropyWindows
23#else
24#ifdef XEN
25 module System.EntropyXen
26 ) where
27import System.EntropyXen
28#else
29 module System.EntropyNix
30 ) where
31import System.EntropyNix
32#endif
33#endif
35import qualified Data.ByteString as B
37-- |Get a specific number of bytes of cryptographically
38-- secure random data using the system-specific facilities.
39--
40-- Use RDRAND if available and XOR with '/dev/urandom' on *nix and CryptAPI when on
41-- Windows. In short, this entropy is considered cryptographically secure
42-- but not true entropy.
43getEntropy :: Int -> IO B.ByteString
44getEntropy n = do
45 h <- openHandle
46 e <- hGetEntropy h n
47 closeHandle h
48 return e