git.iko.soy/typeable/entropy
on main help sign in
Fix the windows code now that a build slave exists.
a8b0d89Thomas M. DuBuisson
System/Entropy.hs+8−12
@@ -22,13 +22,9 @@ module System.Entropy
22 , closeHandle
22 , closeHandle
23 ) where
23 ) where
24
24
25−#ifndef XEN
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)
29−#else
30−import Control.Monad (when)
31−#endif
32import Data.ByteString as B
28import Data.ByteString as B
33
29
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
128 else do
124 else do
129#endif
125#endif
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'
135
131
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
142 else do
138 else do
143#endif
139#endif
144− liftM CH cryptAcquireCtx
140+ liftM CH cryptAcquireCtx
145
141
146-- |Close the `CryptHandle`
142-- |Close the `CryptHandle`
147closeHandle (CH h) = cryptReleaseCtx h
143closeHandle (CH h) = cryptReleaseCtx h
148
144
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)