This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
fradrive-old/src/Crypto/Random/Instances.hs
2020-05-10 17:56:12 +02:00

34 lines
875 B
Haskell

{-# OPTIONS_GHC -fno-warn-orphans #-}
module Crypto.Random.Instances
(
) where
import ClassyPrelude
import Crypto.Random
import System.Random (RandomGen(..))
import qualified Crypto.Error as Crypto
import qualified Data.ByteArray as BA
import Data.Bits
import Data.Binary (Binary)
import qualified Data.Binary as Binary
import Control.Monad.Fail (MonadFail(..))
import qualified Data.ByteString as BS
instance RandomGen ChaChaDRG where
next g = withRandomBytes g (finiteBitSize (maxBound :: Int) `div` 8) (foldr (\x acc -> acc `shiftL` 8 .|. fromIntegral x) zeroBits . BA.unpack @BA.Bytes)
split g = withDRG g drgNew
instance Binary Seed where
put = mapM_ Binary.putWord8 . (BA.convert :: Seed -> ByteString)
get = do
seedBytes <- BS.pack <$> replicateM 40 Binary.getWord8
Crypto.onCryptoFailure (fail . show) return $ seedFromBinary seedBytes