added rotate functions for n rotations of the alphabet

This commit is contained in:
Stefan Friese 2024-05-22 21:58:40 +02:00
parent 778f2c6e7a
commit 146293eeb4
4 changed files with 51 additions and 5 deletions

View File

@ -1,6 +1,16 @@
Revision history for based 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 0.4.2.0 -- 2023-07-25
Changed Base64 decoding to Base64 lenient Changed Base64 decoding to Base64 lenient
Cleanup Cleanup
@ -17,7 +27,13 @@ Revision history for based
0.2.0.0 -- 2022-05-19 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 0.1.0.0 -- 2022-05-17

View File

@ -23,6 +23,7 @@ import Encoding.Xx (encxx, decxx)
import Encoding.QuotedPrintable (encqp, decqp) import Encoding.QuotedPrintable (encqp, decqp)
import Encoding.UnixToUnix (encuu, decuu) import Encoding.UnixToUnix (encuu, decuu)
import Encoding.Yenc (ency, decy) import Encoding.Yenc (ency, decy)
import Encoding.Rotate (rotate)
data Based = Decode { data Based = Decode {
@ -42,6 +43,7 @@ data Based = Decode {
uu :: Bool, uu :: Bool,
xx :: Bool, xx :: Bool,
yenc :: Bool, yenc :: Bool,
rot :: Maybe Int,
solve :: Bool solve :: Bool
} }
| Encode { | Encode {
@ -60,8 +62,9 @@ data Based = Decode {
qp :: Bool, qp :: Bool,
uu :: Bool, uu :: Bool,
xx :: Bool, xx :: Bool,
yenc :: Bool yenc :: Bool,
} rot :: Maybe Int
}
deriving(Show, Data, Typeable) deriving(Show, Data, Typeable)
-- helper functions -- helper functions
@ -175,6 +178,8 @@ optionHandler Decode{xx=True} = decxx
optionHandler Encode{xx=True} = encxx optionHandler Encode{xx=True} = encxx
optionHandler Decode{yenc=True} = decy optionHandler Decode{yenc=True} = decy
optionHandler Encode{yenc=True} = ency optionHandler Encode{yenc=True} = ency
optionHandler Encode{rot=Just n} = rotate n
optionHandler Decode{rot=Just n} = rotate n
optionHandler Decode{solve=True} = solveEnc optionHandler Decode{solve=True} = solveEnc
decodeMode :: Based decodeMode :: Based
@ -195,6 +200,7 @@ decodeMode = Decode {
uu = def &= help "decode uu", uu = def &= help "decode uu",
xx = def &= help "decode xx", xx = def &= help "decode xx",
yenc = def &= help "decode yEncode", yenc = def &= help "decode yEncode",
rot = def &= help "rotate characters by n positions",
solve = def &= help "solve encoding" solve = def &= help "solve encoding"
} &= help "Decode chosen base" &=auto } &= help "Decode chosen base" &=auto
@ -215,8 +221,9 @@ encodeMode = Encode {
qp = def &= help "encode quoted-printable", qp = def &= help "encode quoted-printable",
uu = def &= help "encode uu", uu = def &= help "encode uu",
xx = def &= help "encode xx", 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" } &= help "Encode chosen base"
main :: IO() 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

View File

@ -23,6 +23,7 @@ library
Encoding.UnixToUnix Encoding.UnixToUnix
Encoding.Xx Encoding.Xx
Encoding.Yenc Encoding.Yenc
Encoding.Rotate
other-modules: other-modules:
-- Data.Bytes.Text.Ascii -- Data.Bytes.Text.Ascii
build-depends: build-depends:

22
src/Encoding/Rotate.hs Normal file
View File

@ -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))