From 146293eeb4788b41803cfc551e6001dcaf8d1491 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Wed, 22 May 2024 21:58:40 +0200 Subject: [PATCH] added rotate functions for n rotations of the alphabet --- CHANGELOG.md | 18 +++++++++++++++++- app/Main.hs | 15 +++++++++++---- based.cabal | 1 + src/Encoding/Rotate.hs | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/Encoding/Rotate.hs diff --git a/CHANGELOG.md b/CHANGELOG.md index e673181..222de44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ Revision history for based +0.4.3.0 -- 2024-05-22 + + Added rotate function to encode and decode + Added solver function to decode, to automatically solve some of the encodings, ongoing + Added tests for some of the encode and decode functions, ongoing + Added unicode support for most of the encoding/decoding functions, ongoing + Modularized Encodings to a lib, instead having everything in a single flat file + Cleanup of cabal file + 0.4.2.0 -- 2023-07-25 + Changed Base64 decoding to Base64 lenient Cleanup @@ -17,7 +27,13 @@ Revision history for based 0.2.0.0 -- 2022-05-19 - Added the following bases and encodings * Decimal to hex * Hex to decmial * Xxencoding * UuEncoding * YEncoding * Quoted-Printable + Added the following bases and encodings + * Decimal to hex + * Hex to decmial + * Xxencoding + * UuEncoding + * YEncoding + * Quoted-Printable 0.1.0.0 -- 2022-05-17 diff --git a/app/Main.hs b/app/Main.hs index 31baae2..791176d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -23,6 +23,7 @@ import Encoding.Xx (encxx, decxx) import Encoding.QuotedPrintable (encqp, decqp) import Encoding.UnixToUnix (encuu, decuu) import Encoding.Yenc (ency, decy) +import Encoding.Rotate (rotate) data Based = Decode { @@ -42,6 +43,7 @@ data Based = Decode { uu :: Bool, xx :: Bool, yenc :: Bool, + rot :: Maybe Int, solve :: Bool } | Encode { @@ -60,8 +62,9 @@ data Based = Decode { qp :: Bool, uu :: Bool, xx :: Bool, - yenc :: Bool - } + yenc :: Bool, + rot :: Maybe Int + } deriving(Show, Data, Typeable) -- helper functions @@ -175,6 +178,8 @@ optionHandler Decode{xx=True} = decxx optionHandler Encode{xx=True} = encxx optionHandler Decode{yenc=True} = decy optionHandler Encode{yenc=True} = ency +optionHandler Encode{rot=Just n} = rotate n +optionHandler Decode{rot=Just n} = rotate n optionHandler Decode{solve=True} = solveEnc decodeMode :: Based @@ -195,6 +200,7 @@ decodeMode = Decode { uu = def &= help "decode uu", xx = def &= help "decode xx", yenc = def &= help "decode yEncode", + rot = def &= help "rotate characters by n positions", solve = def &= help "solve encoding" } &= help "Decode chosen base" &=auto @@ -215,8 +221,9 @@ encodeMode = Encode { qp = def &= help "encode quoted-printable", uu = def &= help "encode uu", xx = def &= help "encode xx", - yenc = def &= help "encode yEncode" + yenc = def &= help "encode yEncode", + rot = def &= help "rotate characters by n positions" } &= help "Encode chosen base" main :: IO() -main = cmdArgs (modes[decodeMode, encodeMode] &= help "Anybased, when Cyberchef simply doesn't cut it.\nTo see every parameter of every mode use --help=all" &= program "based" &= summary "based v0.4") >>= interact . optionHandler +main = cmdArgs (modes[decodeMode, encodeMode] &= help "Based, when Cyberchef doesn't cut it.\nTo see every parameter of every mode use --help=all" &= program "based" &= summary "based v0.4") >>= interact . optionHandler diff --git a/based.cabal b/based.cabal index a1a6847..5a64051 100644 --- a/based.cabal +++ b/based.cabal @@ -23,6 +23,7 @@ library Encoding.UnixToUnix Encoding.Xx Encoding.Yenc + Encoding.Rotate other-modules: -- Data.Bytes.Text.Ascii build-depends: diff --git a/src/Encoding/Rotate.hs b/src/Encoding/Rotate.hs new file mode 100644 index 0000000..fde0e53 --- /dev/null +++ b/src/Encoding/Rotate.hs @@ -0,0 +1,22 @@ +module Encoding.Rotate + ( rotate ) where + +import Data.Maybe (fromMaybe) + +upperCase = ['A' .. 'Z'] +lowerCase = ['a' .. 'z'] + +-- rotate :: Int * -> String +rotate :: Int -> String -> String +rotate n s = map (rotchar n) s + where + rotchar n c = + case (lookup c $ transp n lowerCase) of + Just x -> x + Nothing -> fromMaybe c (lookup c $ transp n upperCase) + +transp :: Int -> [Char] -> [(Char, Char)] +transp n x = zip x ((drop n x) ++ (take n x)) + + +