@@ -22,13 +22,9 @@ module System.Entropy
22 , closeHandle
22 , closeHandle
26import Control.Monad (liftM, when)
25import Control.Monad (liftM, when)
27import System.IO.Error (mkIOError, eofErrorType, ioeSetErrorString)
26import System.IO.Error (mkIOError, eofErrorType, ioeSetErrorString)
28import Foreign (allocaBytes)
27import Foreign (allocaBytes)
30−import Control.Monad (when)
32import Data.ByteString as B
28import Data.ByteString as B
34#if defined(isWindows)
30#if defined(isWindows)
@@ -127,11 +123,11 @@ getEntropy n = do
127 if b then hGetEntropy UseRdRand n
123 if b then hGetEntropy UseRdRand n
130− h <- cryptAcquireCtx
126+ h <- cryptAcquireCtx
131− bs <- cryptGenRandom h n
127+ bs <- cryptGenRandom h n
132− let !bs' = bs
128+ let !bs' = bs
133− cryptReleaseCtx h
129+ cryptReleaseCtx h
134− return bs'
130+ return bs'
136-- |Open a handle from which random data can be read
132-- |Open a handle from which random data can be read
137openHandle :: IO CryptHandle
133openHandle :: IO CryptHandle
@@ -141,16 +137,16 @@ openHandle = do
141 if b then return UseRdRand
137 if b then return UseRdRand
144− liftM CH cryptAcquireCtx
140+ liftM CH cryptAcquireCtx
146-- |Close the `CryptHandle`
142-- |Close the `CryptHandle`
147closeHandle (CH h) = cryptReleaseCtx h
143closeHandle (CH h) = cryptReleaseCtx h
149-- |Read from `CryptHandle`
145-- |Read from `CryptHandle`
150hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
146hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
151−hGetEntropy (CH h) = cryptGenRandom h
147+hGetEntropy (CH h) n = cryptGenRandom h n
152#ifdef HAVE_RDRAND
148#ifdef HAVE_RDRAND
153−hGetEntropy UseRdRand =
149+hGetEntropy UseRdRand n =
154 B.create n $ \ptr -> do
150 B.create n $ \ptr -> do
155 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
151 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
156 when (r /= 0)
152 when (r /= 0)