ppad-random-0.1.0: lib/Crypto/Entropy.hs
{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module: Crypto.Entropy
-- Copyright: (c) 2026 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Entropy from the operating system, via getentropy(3).
module Crypto.Entropy (
fill
) where
import Data.Word (Word8)
import Foreign.C.Types (CInt(..), CSize(..))
import Foreign.Ptr (Ptr)
-- getentropy may block until the system CSPRNG is seeded, so the call
-- is safe rather than unsafe
foreign import ccall safe "getentropy"
c_getentropy :: Ptr Word8 -> CSize -> IO CInt
-- | Fill the destination with the given number of bytes (at most 256)
-- of entropy from the operating system, returning 'False' on
-- failure.
fill :: Ptr Word8 -> Int -> IO Bool
fill dst n = do
r <- c_getentropy dst (fromIntegral n)
pure $! r == 0