-- | Entropy based on `/dev/urandom`.
module Raaz.Entropy( getEntropy ) where

import Control.Monad.IO.Class( MonadIO, liftIO)
import System.IO
import Raaz.Core


-- | Get random bytes from the system. Do not over use this function
-- as it is meant to be used by a PRG. This function reads bytes from
-- '/dev/urandom'.
getEntropy :: (MonadIO m, LengthUnit l) => l -> Pointer -> m (BYTES Int)
getEntropy :: l -> Pointer -> m (BYTES Int)
getEntropy l
l Pointer
ptr = IO (BYTES Int) -> m (BYTES Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (BYTES Int) -> m (BYTES Int))
-> IO (BYTES Int) -> m (BYTES Int)
forall a b. (a -> b) -> a -> b
$ FilePath -> IOMode -> (Handle -> IO (BYTES Int)) -> IO (BYTES Int)
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
"/dev/urandom" IOMode
ReadMode ((Handle -> IO (BYTES Int)) -> IO (BYTES Int))
-> (Handle -> IO (BYTES Int)) -> IO (BYTES Int)
forall a b. (a -> b) -> a -> b
$ \ Handle
hand -> Handle -> Pointer -> l -> IO (BYTES Int)
forall bufSize.
LengthUnit bufSize =>
Handle -> Pointer -> bufSize -> IO (BYTES Int)
hFillBuf Handle
hand Pointer
ptr l
l