cryptopals

Matasano's cryptopals challenges (cryptopals.com).
git clone git://git.jtobin.io/cryptopals.git
Log | Files | Refs | README | LICENSE

MT19937.hs (901B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 {-# LANGUAGE RecordWildCards #-}
      3 
      4 module Main where
      5 
      6 import qualified Cryptopals.Stream.RNG.MT19937 as MT
      7 import qualified Data.Text as T
      8 import qualified Data.Text.IO as TIO
      9 import Data.Foldable (for_)
     10 import GHC.Word (Word32)
     11 import qualified Options.Applicative as O
     12 
     13 data Args = Args {
     14     argsSeed  :: Word32
     15   , argsBytes :: Word32
     16   }
     17 
     18 ops :: O.Parser Args
     19 ops = Args
     20   <$> O.argument O.auto (O.metavar "SEED")
     21   <*> O.argument O.auto (O.metavar "BYTES")
     22 
     23 mt :: Args -> IO ()
     24 mt Args {..} = do
     25   let gen        = MT.seed argsSeed
     26       (bytes, _) = MT.tap (fromIntegral argsBytes) gen
     27 
     28   for_ bytes $ TIO.putStrLn . T.pack . show
     29 
     30 main :: IO ()
     31 main = do
     32   let pars = O.info (O.helper <*> ops) $
     33            O.fullDesc
     34         <> O.progDesc "generate random bytes from a Mersenne Twister"
     35         <> O.header "mt19937"
     36 
     37   args <- O.execParser pars
     38 
     39   mt args
     40