summaryrefslogtreecommitdiff
path: root/base64/base64_0.hs
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2019-03-17 11:16:40 +0100
committerMiguel <m.i@gmx.at>2019-03-17 11:16:40 +0100
commitb423baf7176808b134ca45a6b19a990530853785 (patch)
tree0f049ed8a03b700888d824d1a70d4e3b48f10514 /base64/base64_0.hs
parent9dc8556d2510b125e3449e4eceac1eb126f9179e (diff)
sort base64/
Diffstat (limited to 'base64/base64_0.hs')
-rw-r--r--base64/base64_0.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/base64/base64_0.hs b/base64/base64_0.hs
new file mode 100644
index 0000000..f7673b8
--- /dev/null
+++ b/base64/base64_0.hs
@@ -0,0 +1,25 @@
+-- File: base64.hs --
+
+import Data.Char
+import Text.Printf
+import qualified Data.List as L
+import qualified Data.List.Split as T
+
+toBase64 x = maskBase64 x . toBase64core . asciiToBin . binFill $ x
+toBase64core = map base64toDigit . map binToDec . T.chunksOf 6
+
+base64toDigit x = (['A'..'Z']++['a'..'z']++['0'..'9']++['+','/']) !! x
+binToDec = sum . map (2^) . L.findIndices (=='1') . reverse
+asciiToBin = concat . map (\y -> printf "%08b" y) . map ord
+binFill x = x ++ (take (fill64length x) $ cycle "\000")
+
+maskBase64 o x = take (length x - l ) x ++ (take l $ cycle "=")
+ where l = (fill64length o)
+
+fill64length x | m==0 = 0
+ | otherwise = 3-m
+ where m=mod (length x) 3
+
+main = do
+ line <- getLine
+ putStrLn $ toBase64 line