git.iko.soy/typeable/entropy
on main help sign in
Acquire random bytes till the request is filled.

This is untested, I'm going to perform a similar test as in #19 then bump and upload to hackage.

commit3b2012828a4a758fa1fc3932d0c46b5adf651ca4 authorThomas M. DuBuisson <tommd@galois.com> dateWed 22 Oct 2014, 11:04 (12 years ago) parentb05a6d8
1 file changed+8−4
System/EntropyNix.hs+8−4
System/EntropyNix.hs+8−4
@@ -77,12 +77,16 @@ hGetEntropy (UseRdRand h) = \n ->
7777#endif
7878
7979fdReadBS :: Fd -> Int -> IO B.ByteString
80−fdReadBS fd n = do
81− allocaBytes n $ \buf -> do
82− rc <- fdReadBuf fd buf (fromIntegral n)
80+fdReadBS fd n =
81+ allocaBytes n $ \buf -> go buf (fromIntegral n)
82+ where
83+ go buf 0 = B.packCStringLen (castPtr buf, fromIntegral n)
84+ go buf cnt | cnt < n = do
85+ rc <- fdReadBuf fd (plusPtr buf (n - cnt)) (fromIntegral cnt)
8386 case rc of
8487 0 -> ioError (ioeSetErrorString (mkIOError eofErrorType "fdRead" Nothing Nothing) "EOF")
85− n' -> B.packCStringLen (castPtr buf, fromIntegral n')
88+ n' -> go buf (cnt - fromIntegral n')
89+ go _ _ = error "Impossible! The count of bytes left to read is greater than the request or less than zero!"
8690
8791#ifdef HAVE_RDRAND
8892foreign import ccall unsafe "cpu_has_rdrand"