cprng-aes 0.1.0 → 0.2.0
raw patch · 2 files changed
+14/−12 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Crypto.Random.AESCtr: getRandomBytes :: AESRNG -> Int -> (ByteString, AESRNG)
+ Crypto.Random.AESCtr: genRandomBytes :: AESRNG -> Int -> (ByteString, AESRNG)
Files
- Crypto/Random/AESCtr.hs +13/−11
- cprng-aes.cabal +1/−1
Crypto/Random/AESCtr.hs view
@@ -5,19 +5,19 @@ -- Stability : stable -- Portability : unknown ----- this CPRNG is an AES cbc based counter system.+-- this CPRNG is an AES based counter system. -- -- the internal size of fields are: 16 bytes IV, 16 bytes counter, 32 bytes key -- -- each block are generated the following way:--- (IV `xor` counter) `aes` key -> 16 bytes output+-- aes (IV `xor` counter) -> 16 bytes output -- module Crypto.Random.AESCtr ( AESRNG , make , makeSystem- , getRandomBytes+ , genRandomBytes ) where import Control.Applicative ((<$>))@@ -57,9 +57,11 @@ (cnt, left2) = B.splitAt 16 left1 (iv, left1) = B.splitAt 16 b --- | make an AESRNG from a bytestring. the bytestring need to be at least 64 bytes.+-- | make an AES RNG from a bytestring seed. the bytestring need to be at least 64 bytes. -- if the bytestring is longer, the extra bytes will be ignored and will not take part in -- the initialization.+--+-- use `makeSystem` to not have to deal with the generator seed. make :: B.ByteString -> Either GenError AESRNG make b | B.length b < 64 = Left NotEnoughEntropy@@ -77,10 +79,10 @@ nextChunk (RNG iv counter key) = (chunk, newrng) where newrng = RNG chunk (add1 counter) key- chunk = AES.encryptCBC key iv bytes+ chunk = AES.encrypt key bytes bytes = iv `bxor` (put128 counter) --- | Initialize a new AESRng using the system entropy.+-- | Initialize a new AES RNG using the system entropy. makeSystem :: IO AESRNG makeSystem = ofRight . make <$> getEntropy 64 where@@ -88,10 +90,10 @@ ofRight (Right x) = x -- | get a Random number of bytes from the RNG.--- for efficienty and not wasted any randomness, it's better to generate--- bytes on multiple of 16, however it will works for any size.-getRandomBytes :: AESRNG -> Int -> (ByteString, AESRNG)-getRandomBytes rng n =+-- it generate randomness by block of 16 bytes, but will truncate+-- to the number of bytes required, and lose the truncated bytes.+genRandomBytes :: AESRNG -> Int -> (ByteString, AESRNG)+genRandomBytes rng n = let list = helper rng n in (B.concat $ map fst list, snd $ last list) where@@ -105,7 +107,7 @@ instance CryptoRandomGen AESRNG where newGen = make genSeedLength = 64- genBytes len rng = Right $ getRandomBytes rng len+ genBytes len rng = Right $ genRandomBytes rng len reseed b rng@(RNG _ cnt1 _) | B.length b < 64 = Left NotEnoughEntropy | otherwise = Right $ RNG (r16 `bxor` iv2) (get128 (put128 cnt1 `bxor` cnt2)) key2
cprng-aes.cabal view
@@ -1,5 +1,5 @@ Name: cprng-aes-Version: 0.1.0+Version: 0.2.0 Description: Simple crypto pseudo-random-number-generator with really good randomness property. .