From edd0c7ffd59818995a0af7d9f24213ec9bd8e217 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sun, 5 May 2024 00:04:19 +0200 Subject: [PATCH] added more bases to the solver functions --- Main.hs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Main.hs b/Main.hs index 6cef2e2..916efcf 100644 --- a/Main.hs +++ b/Main.hs @@ -5,8 +5,11 @@ module Main where import System.Console.CmdArgs import Control.Arrow import Data.Text (pack, unpack) +import qualified Data.Text as T +import qualified Data.Text.Lazy as TL +import Data.Text.Lazy.Builder (toLazyText) import Data.Text.Encoding (encodeUtf8) -import Data.Maybe (fromJust) +import Data.Maybe (fromJust, fromMaybe) import TextShow (toText) import Text.XML.HXT.DOM.Util (hexStringToInt, intToHexString, decimalStringToInt) import TextShow.Data.Integral (showbBin, showbOct) @@ -40,7 +43,6 @@ import qualified Data.ByteString.Char8 as C import Text.Regex.TDFA import Data.Word (Word8) import Data.Char (ord, chr) -import Data.Maybe (fromMaybe) data Based = Decode { b91 :: Bool, @@ -122,13 +124,13 @@ 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 :: String -> String --- dec91 = C.unpack . B91.decode - dec91 :: String -> String -dec91 input = - case B91.decode input :: BSU.ByteString of - decoded -> C.unpack decoded +dec91 = C.unpack . B91.decode + +-- dec91 :: String -> String +-- dec91 input = +-- case B91.decode input :: BSU.ByteString of +-- decoded -> C.unpack decoded -- toWord8 :: String -> [Word8] -- toWord8 = map (fromIntegral . fromEnum) @@ -230,7 +232,12 @@ enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depen 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 + +dec2 :: String -> String +-- dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt +dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt + +enc2 :: String -> String enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit))) decqp :: String -> String @@ -275,25 +282,34 @@ ency :: String -> String ency = C.unpack . Y.encode . BSU.fromString base91Regex = "^[!-~]*$" +base85Regex = "^[0-9A-Za-z!#$%&()*+,-;<=>?@^_`{|}~]+$" -- base85Regex = "^[A-Za-u0-9!\"#$%&((*+,-./;:<=@[]\\`]*$" base64Regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$" base58Regex = "^[1-9A-HJ-NP-Za-km-z]+$" -- incorrect base32Regex = "^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=)?$" base16Regex = "^[0-9A-FXx]*$" +base2Regex = "^[01]*$" +urlRegex = "^[a-zA-Z0-9%]*$" solveEnc :: String -> String solveEnc input = let isBase91 = BSU.fromString input =~ base91Regex :: Bool + isBase85 = BSU.fromString input =~ base85Regex :: Bool isBase64 = BSU.fromString input =~ base64Regex :: Bool isBase58 = BSU.fromString input =~ base58Regex :: Bool isBase32 = BSU.fromString input =~ base32Regex :: Bool isBase16 = BSU.fromString input =~ base16Regex :: Bool + isBase2 = BSU.fromString input =~ base2Regex :: Bool + isURL = BSU.fromString input =~ urlRegex :: Bool base91Result = if isBase91 then "\nTrying base91:\n" ++ dec91 input else "" + base85Result = if isBase85 then "\nTrying base85:\n" ++ dec85 input else "" base64Result = if isBase64 then "\nTrying base64:\n" ++ dec64 input else "" base58Result = if isBase58 then "\nTrying base58:\n" ++ dec58 input else "" base32Result = if isBase64 then "\nTrying base32:\n" ++ dec32 input else "" base16Result = if isBase16 then "\nTrying base16:\n" ++ dec16 input else "" - results = filter (not . null) [base91Result, base64Result, base32Result, base16Result] + base2Result = if isBase2 then "\nTrying base2:\n" ++ dec2 input else "" + urlResult = if isURL then "\nTrying URL decode:\n" ++ decurl input else "" + results = filter (not . null) [base91Result, base85Result, base64Result, base58Result, base32Result, base16Result, base2Result, urlResult] in if null results then "Not able to solve the encoding.\n" @@ -337,8 +353,8 @@ 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 -optionHandler Decode{uu=True} = encuu +optionHandler Encode{uu=True} = encuu +optionHandler Decode{uu=True} = decuu optionHandler Decode{xx=True} = decxx optionHandler Encode{xx=True} = encxx optionHandler Decode{yenc=True} = decy