cryptopals

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

ByteFrequency.hs (1158B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 {-# LANGUAGE RecordWildCards #-}
      3 
      4 module Main where
      5 
      6 import qualified Cryptopals.Util as CU
      7 import qualified Data.ByteString.Base16 as B16
      8 import qualified Data.Text as T
      9 import qualified Data.Text.IO as TIO
     10 import qualified Data.Text.Encoding as TE
     11 import qualified Options.Applicative as O
     12 import qualified System.Exit as SE
     13 import qualified System.IO as SIO
     14 
     15 data Args = Args { argsInp :: T.Text }
     16 
     17 ops :: O.Parser Args
     18 ops = Args <$> O.argument O.str (O.metavar "INPUT")
     19 
     20 freq :: Args -> IO ()
     21 freq Args {..} = do
     22   let render :: Show a => a -> T.Text
     23       render = T.pack . show
     24 
     25       err = TIO.hPutStrLn SIO.stderr
     26 
     27       args = B16.decodeBase16 $ TE.encodeUtf8 argsInp
     28 
     29   case args of
     30     Left e -> do
     31       err $ "cryptopals: " <> e
     32       SE.exitFailure
     33 
     34     Right s -> do
     35       let freqs = take 3 $ CU.often s
     36 
     37       err $ "cryptopals: common bytes (" <> argsInp <> ")"
     38       err $ render freqs
     39 
     40 main :: IO ()
     41 main = do
     42   let pars = O.info (O.helper <*> ops) $
     43            O.fullDesc
     44         <> O.progDesc "produce byte frequencies"
     45         <> O.header "byte-frequency"
     46 
     47   args <- O.execParser pars
     48 
     49   freq args
     50