From 4c6f3775a5865d06b66a002862e7c90e101400d8 Mon Sep 17 00:00:00 2001 From: whx Date: Sun, 22 May 2022 22:48:44 +0200 Subject: [PATCH] added base64url, oct, binary --- Main.hs | 48 +++++++++++++++++++++++++++++++++++++++++------- based.cabal | 6 +++--- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Main.hs b/Main.hs index a333e8c..9413dc1 100644 --- a/Main.hs +++ b/Main.hs @@ -4,18 +4,19 @@ module Main where import System.Console.CmdArgs import Control.Arrow -import Numeric (showHex, showIntAtBase) import Data.Text (pack, unpack) -import Data.Char (intToDigit, ord, chr) +import Data.Text.Encoding (encodeUtf8) import Data.Maybe (fromJust) import TextShow (toText) -import Text.XML.HXT.DOM.Util (stringToHexString, hexStringToInt, intToHexString, charToHexString, decimalStringToInt) -import TextShow.Data.Integral (showbBin, showbHex) +import Text.XML.HXT.DOM.Util (hexStringToInt, intToHexString, decimalStringToInt) +import TextShow.Data.Integral (showbBin, showbOct) +import Text.Ascii (fromBinDigit, fromOctDigit) import Codec.CBOR.Magic (intToWord64) import qualified Data.Either.Unwrap as U import qualified Codec.Binary.Base91 as B91 import qualified Codec.Binary.Base85 as B85 import qualified Codec.Binary.Base64 as B64 +import qualified Codec.Binary.Base64Url as B64U import qualified Data.Word.Base62 as B62 import qualified Haskoin.Address.Base58 as B58 import qualified Codec.Binary.Base32 as B32 @@ -34,11 +35,14 @@ data Based = Decode { b91 :: Bool, b85 :: Bool, b64 :: Bool, + b64url :: Bool, b62 :: Bool, b58 :: Bool, b32 :: Bool, b16 :: Bool, b10 :: Bool, + b8 :: Bool, + b2 :: Bool, qp :: Bool, uu :: Bool, xx :: Bool, @@ -48,11 +52,14 @@ data Based = Decode { b91 :: Bool, b85 :: Bool, b64 :: Bool, + b64url :: Bool, b62 :: Bool, b58 :: Bool, b32 :: Bool, b16 :: Bool, b10 :: Bool, + b8 :: Bool, + b2 :: Bool, qp :: Bool, uu :: Bool, xx :: Bool, @@ -60,6 +67,16 @@ data Based = Decode { } deriving(Show, Data, Typeable) +-- helper functions +binToInt :: [Int] -> Int +binToInt [] = 0 +binToInt (x : xs) = x + 2 * binToInt xs + +octToInt :: [Int] -> Int +octToInt [] = 0 +octToInt (x : xs) = x + 8 * octToInt xs + +-- base functions -- without the show func, sequences like \n will not be shown as characters but will be executed as newline dec91 = C.unpack . B91.decode enc91 = B91.encode . BSU.fromString @@ -67,6 +84,8 @@ dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString enc85 = C.unpack . B85.encode . BSU.fromString dec64 = C.unpack . U.fromRight . B64.decode . BSU.fromString enc64 = C.unpack . B64.encode . BSU.fromString +dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString +enc64url = C.unpack . B64U.encode . BSU.fromString dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString) enc62 = C.unpack . BSU.fromString. Bytes.toLatinString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) dec58 = C.unpack . fromJust . B58.decodeBase58 . pack @@ -77,6 +96,10 @@ dec16 = C.unpack . U.fromRight . B16.decode . BSU.fromString enc16 = C.unpack . B16.encode . BSU.fromString dec10 = show . hexStringToInt enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord +dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt +enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit))) +dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt +enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit))) decqp = C.unpack . U.fromRight . QP.decode . BSU.fromString encqp = C.unpack . QP.encode . BSU.fromString decuu = C.unpack . U.fromRight . UU.decode . BSU.fromString @@ -86,13 +109,14 @@ encxx = C.unpack . XX.encode . BSU.fromString decy = C.unpack . U.fromRight . Y.decode . BSU.fromString ency = C.unpack . Y.encode . BSU.fromString - optionHandler Decode{b91=True} = dec91 optionHandler Encode{b91=True} = enc91 optionHandler Decode{b85=True} = dec85 optionHandler Encode{b85=True} = enc85 optionHandler Decode{b64=True} = dec64 optionHandler Encode{b64=True} = enc64 +optionHandler Decode{b64url=True} = dec64url +optionHandler Encode{b64url=True} = enc64url optionHandler Decode{b62=True} = dec62 optionHandler Encode{b62=True} = enc62 optionHandler Decode{b58=True} = dec58 @@ -103,6 +127,10 @@ optionHandler Decode{b16=True} = dec16 optionHandler Encode{b16=True} = enc16 optionHandler Decode{b10=True} = dec10 optionHandler Encode{b10=True} = enc10 +optionHandler Decode{b8=True} = dec8 +optionHandler Encode{b8=True} = enc8 +optionHandler Decode{b2=True} = dec2 +optionHandler Encode{b2=True} = enc2 optionHandler Decode{qp=True} = decqp optionHandler Encode{qp=True} = encqp optionHandler Encode{uu=True} = decuu @@ -116,11 +144,14 @@ decode_ = Decode { b91 = def &= help "decode base91", b85 = def &= help "decode base85", b64 = def &= help "decode base64", + b64url = def &= help "decode base64Url", b62 = def &= help "decode base62", b58 = def &= help "decode base58", b32 = def &= help "decode base32", b16 = def &= help "decode base16", - b10 = def &= help "decode base10 to hex", + b10 = def &= help "decode decimal from hex", + b8 = def &= help "decode octal from hex", + b2 = def &= help "decode binary from hex", qp = def &= help "decode quoted-printable", uu = def &= help "decode uu", xx = def &= help "decode xx", @@ -131,15 +162,18 @@ encode_ = Encode { b91 = def &= help "encode base91", b85 = def &= help "encode base85", b64 = def &= help "encode base64", + b64url = def &= help "encode base64Url", b62 = def &= help "encode base62", b58 = def &= help "encode base58", b32 = def &= help "encode base32", b16 = def &= help "encode base16", b10 = def &= help "encode base10 to hex", + b8 = def &= help "encode octal to hex", + b2 = def &= help "encode binary to hex", qp = def &= help "encode quoted-printable", uu = def &= help "encode uu", xx = def &= help "encode xx", yenc = def &= help "encode yEncode" } &= help "Encode chosen base" -main = cmdArgs (modes[decode_, encode_] &= help "Anybased, when Cyberchef simply doesn't cut it. " &= program "based" &= summary "based v0.3") >>= interact . optionHandler +main = cmdArgs (modes[decode_, encode_] &= 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 diff --git a/based.cabal b/based.cabal index 90d4665..324ff25 100644 --- a/based.cabal +++ b/based.cabal @@ -3,7 +3,7 @@ cabal-version: 2.4 -- For further documentation, see http://haskell.org/cabal/users-guide/ name: based -version: 0.1.0.0 +version: 0.4.0.0 -- synopsis: -- description: -- bug-reports: @@ -18,7 +18,7 @@ extra-source-files: CHANGELOG.md library exposed-modules: MyLib -- other-modules: - -- other-extensions: + -- other-extensions build-depends: base ^>=4.13.0.0 -- hs-source-dirs: default-language: Haskell2010 @@ -27,6 +27,6 @@ executable based main-is: Main.hs other-modules: MyLib -- other-extensions: - build-depends: base ^>=4.13.0.0, based, cmdargs, sandi, base62, base91, utf8-string, bytestring, byteslice, either-unwrap, text-show, hxt,haskoin-core, text, bytes, cborg + build-depends: base ^>=4.13.0.0, based, cmdargs, sandi, base62, base91, utf8-string, bytestring, byteslice, either-unwrap, text-show, hxt,haskoin-core, text, bytes, cborg, text-latin1 -- hs-source-dirs: default-language: Haskell2010