cryptopals

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

Rotate.hs (1182B)


      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.Encoding as TE
     10 import qualified Data.Text.IO as TIO
     11 import Data.Foldable (for_)
     12 import qualified Options.Applicative as O
     13 import qualified System.Exit as SE
     14 import qualified System.IO as SIO
     15 
     16 data Args = Args {
     17     argsSiz :: Int
     18   , argsInp :: T.Text
     19   }
     20 
     21 ops :: O.Parser Args
     22 ops = Args
     23   <$> O.argument O.auto (O.metavar "ROWS")
     24   <*> O.argument O.str (O.metavar "INPUT")
     25 
     26 rot :: Args -> IO ()
     27 rot Args {..} = do
     28   let args = do
     29         v <- B16.decodeBase16 $ TE.encodeUtf8 argsInp
     30         pure (argsSiz, v)
     31 
     32   case args of
     33     Left e -> do
     34       TIO.hPutStrLn SIO.stderr ("cryptopals: " <> e)
     35       SE.exitFailure
     36 
     37     Right (s, v) -> do
     38       let res = CU.rotate s v
     39       for_ res $ TIO.putStrLn . TE.decodeUtf8 . B16.encodeBase16'
     40 
     41 main :: IO ()
     42 main = do
     43   let pars = O.info (O.helper <*> ops) $
     44            O.fullDesc
     45         <> O.progDesc "transpose the target bytes"
     46         <> O.header "rotate"
     47 
     48   args <- O.execParser pars
     49 
     50   rot args
     51