Created
February 25, 2016 12:48
-
-
Save sajith/fe898efa3da21f28161a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE PackageImports #-} | |
-- Fails with "*** Exception: NeedsInfiniteSeed" | |
module RandomStr2 where | |
import "crypto-api" Crypto.Random | |
import "crypto-api" Crypto.Types | |
import Data.ByteString.Char8 as B | |
import System.Entropy | |
getGen :: CryptoRandomGen g => Int -> IO g | |
getGen len = do | |
entropy <- getEntropy len | |
case newGen entropy of | |
Left err -> error (show err) | |
Right g' -> return g' | |
makeStr:: CryptoRandomGen g => ByteLength -> g -> ByteString | |
makeStr len g = | |
case genBytes len g of | |
Left err -> B.empty | |
Right (bs, _) -> bs | |
main :: IO () | |
main = do | |
let len = 12 | |
g <- getGen len :: IO SystemRandom | |
let str = makeStr len g | |
B.putStrLn str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment