types for enc/dec b91

This commit is contained in:
Stefan Friese 2024-04-28 23:47:31 +02:00
parent 27e600673b
commit 68eb560810
1 changed files with 34 additions and 22 deletions

56
Main.hs
View File

@ -79,18 +79,18 @@ data Based = Decode {
-- Just bs -> Right bs -- Just bs -> Right bs
-- Nothing -> Left "Failed to convert string to ByteString." -- Nothing -> Left "Failed to convert string to ByteString."
-- decodeFromBase91 :: C.ByteString -> Either String BS.ByteString -- decodeFromBase91 :: String -> Either String C.ByteString
-- decodeToBase91 decoded = -- decodeFromBase91 decoded =
-- case B91.decode of -- case B91.decode of
-- Just bs -> Right decoded -- decoded | C.null decoded -> Left "Failed to decode from base91"
-- Nothing -> Left "Failed to decode from Base91." -- | otherwise -> Right decoded
-- encodeToBase64 :: C.ByteString -> Either String BS.ByteString encodeToBase64 :: C.ByteString -> Either String C.ByteString
-- encodeToBase64 bs = encodeToBase64 bs =
-- case B64.encode bs of case B64.encode bs of
-- encoded | BS.null encoded -> Left "Failed to encode base64." encoded | C.null encoded -> Left "Failed to encode base64."
-- | otherwise -> Right encoded | otherwise -> Right encoded
decodeFromBase64 :: C.ByteString -> Either String C.ByteString decodeFromBase64 :: C.ByteString -> Either String C.ByteString
decodeFromBase64 bs = decodeFromBase64 bs =
@ -112,15 +112,26 @@ octToInt (x : xs) = x + 8 * octToInt xs
-- base functions -- base functions
-- without the show func, sequences like \n will not be shown as characters but will be executed as newline -- 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 = C.unpack . B91.decode
-- dec91 :: C.ByteString -> Either String String
-- dec91 input = do -- dec91 :: String -> String
-- decoded <- decodeFromBase91 input -- dec91 input =
-- -- return $ C.unpack decoded -- case B91.decode (C.pack input) of
-- -- is the same as -- Right decoded -> C.unpack decoded
-- return (C.unpack decoded) -- Left errMsg -> "Error: " ++ errMsg
-- decoded | C.null decoded -> Left "Failed to decode from Base91"
-- | otherwise -> Right (C.unpack decoded)
enc91 :: String -> String
enc91 = B91.encode . BSU.fromString enc91 = B91.encode . BSU.fromString
-- enc91 :: String -> Either String String
-- enc91 input =
-- case B91.encode (BSU.fromString input) of
-- encoded | C.null encoded -> Left "Failed to encode to Base91"
-- | otherwise -> Right (C.unpack encoded)
dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString
enc85 = C.unpack . B85.encode . BSU.fromString enc85 = C.unpack . B85.encode . BSU.fromString
@ -131,12 +142,13 @@ dec64 input =
Right byteString -> C.unpack byteString Right byteString -> C.unpack byteString
Left errMsg -> "Error: " ++ errMsg Left errMsg -> "Error: " ++ errMsg
enc64 = C.unpack . B64.encode . BSU.fromString -- enc64 = C.unpack . B64.encode . BSU.fromString
-- enc64 :: String -> Either String String enc64 :: String -> String
-- enc64 input = do enc64 input =
-- byteString <- convertToByteString input case encodeToBase64 (BSU.fromString input) of
-- encoded <- encodeToBase64 byteString Right byteString -> C.unpack byteString
-- return $ BS.unpack encoded Left errMsg -> "Error: " ++ errMsg
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
enc64url = C.unpack . B64U.encode . BSU.fromString enc64url = C.unpack . B64U.encode . BSU.fromString