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
-- Nothing -> Left "Failed to convert string to ByteString."
-- decodeFromBase91 :: C.ByteString -> Either String BS.ByteString
-- decodeToBase91 decoded =
-- decodeFromBase91 :: String -> Either String C.ByteString
-- decodeFromBase91 decoded =
-- case B91.decode of
-- Just bs -> Right decoded
-- Nothing -> Left "Failed to decode from Base91."
-- decoded | C.null decoded -> Left "Failed to decode from base91"
-- | otherwise -> Right decoded
-- encodeToBase64 :: C.ByteString -> Either String BS.ByteString
-- encodeToBase64 bs =
-- case B64.encode bs of
-- encoded | BS.null encoded -> Left "Failed to encode base64."
-- | otherwise -> Right encoded
encodeToBase64 :: C.ByteString -> Either String C.ByteString
encodeToBase64 bs =
case B64.encode bs of
encoded | C.null encoded -> Left "Failed to encode base64."
| otherwise -> Right encoded
decodeFromBase64 :: C.ByteString -> Either String C.ByteString
decodeFromBase64 bs =
@ -112,15 +112,26 @@ 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 :: C.ByteString -> Either String String
-- dec91 input = do
-- decoded <- decodeFromBase91 input
-- -- return $ C.unpack decoded
-- -- is the same as
-- return (C.unpack decoded)
-- dec91 :: String -> String
-- dec91 input =
-- case B91.decode (C.pack input) of
-- Right decoded -> 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 :: 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
enc85 = C.unpack . B85.encode . BSU.fromString
@ -131,12 +142,13 @@ dec64 input =
Right byteString -> C.unpack byteString
Left errMsg -> "Error: " ++ errMsg
enc64 = C.unpack . B64.encode . BSU.fromString
-- enc64 :: String -> Either String String
-- enc64 input = do
-- byteString <- convertToByteString input
-- encoded <- encodeToBase64 byteString
-- return $ BS.unpack encoded
-- enc64 = C.unpack . B64.encode . BSU.fromString
enc64 :: String -> String
enc64 input =
case encodeToBase64 (BSU.fromString input) of
Right byteString -> C.unpack byteString
Left errMsg -> "Error: " ++ errMsg
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
enc64url = C.unpack . B64U.encode . BSU.fromString