From ff361643054f17373b22d8248f80138cb6ed951f Mon Sep 17 00:00:00 2001 From: Tobias Florek Date: Fri, 12 Jun 2015 20:52:15 +0200 Subject: [PATCH] copy withRandomBytes from crypto-random --- Crypto/Random.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Crypto/Random.hs b/Crypto/Random.hs index d270b82..a8cffcc 100644 --- a/Crypto/Random.hs +++ b/Crypto/Random.hs @@ -13,6 +13,7 @@ module Crypto.Random , drgNew , drgNewTest , withDRG + , withRandomBytes , DRG(..) -- * Random abstraction , MonadRandom(..) @@ -22,7 +23,7 @@ module Crypto.Random import Crypto.Random.Types import Crypto.Random.ChaChaDRG import Crypto.Random.Entropy -import Data.ByteArray (ScrubbedBytes) +import Data.ByteArray (ByteArray, ScrubbedBytes) import Crypto.Internal.Imports -- | Create a new DRG from system entropy @@ -38,3 +39,10 @@ drgNew = initialize <$> (getEntropy 40 :: IO ScrubbedBytes) -- has been properly randomly generated. drgNewTest :: (Word64, Word64, Word64, Word64, Word64) -> ChaChaDRG drgNewTest = initializeWords + +-- | Generate @len random bytes and mapped the bytes to the function @f. +-- +-- This is equivalent to use Control.Arrow 'first' with 'randomBytesGenerate' +withRandomBytes :: (ByteArray ba, DRG g) => g -> Int -> (ba -> a) -> (a, g) +withRandomBytes rng len f = (f bs, rng') + where (bs, rng') = randomBytesGenerate len rng