module OpenSSL.Random
(
randBytes
, prandBytes
, add
) where
import Foreign
import Foreign.C.Types
import qualified Data.ByteString as BS
import OpenSSL.Utils
foreign import ccall unsafe "RAND_bytes"
_RAND_bytes :: Ptr CChar -> CInt -> IO CInt
foreign import ccall unsafe "RAND_pseudo_bytes"
_RAND_pseudo_bytes :: Ptr CChar -> CInt -> IO ()
foreign import ccall unsafe "RAND_add"
_RAND_add :: Ptr CChar -> CInt -> CInt -> IO ()
randBytes :: Int
-> IO BS.ByteString
randBytes n =
allocaArray n $ \bufPtr ->
do _RAND_bytes bufPtr (fromIntegral n) >>= failIf_ (/= 1)
BS.packCStringLen (bufPtr, n)
prandBytes :: Int
-> IO BS.ByteString
prandBytes n =
allocaArray n $ \bufPtr ->
do _RAND_pseudo_bytes bufPtr (fromIntegral n)
BS.packCStringLen (bufPtr, n)
add :: BS.ByteString
-> Int
-> IO ()
add bs entropy =
BS.useAsCStringLen bs $ \(ptr, len) ->
_RAND_add ptr (fromIntegral len) (fromIntegral entropy)