Skip to content

Instantly share code, notes, and snippets.

@sajith
Created February 25, 2016 12:48
Show Gist options
  • Save sajith/fe898efa3da21f28161a to your computer and use it in GitHub Desktop.
Save sajith/fe898efa3da21f28161a to your computer and use it in GitHub Desktop.
{-# 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