diff options
| author | Miguel <m.i@gmx.at> | 2019-02-28 17:24:44 +0100 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2019-02-28 17:24:44 +0100 |
| commit | a97b3edf385a1fd69899ea8fe23889fa0d206079 (patch) | |
| tree | 50f528dccdd8830561a1b8a718131f33a3afb86c | |
| parent | 95fd10974e661499a4bb2bf68b5498468d62e118 (diff) | |
base64 encoder and decoder (text-only)
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | base64/base64.hs | 45 |
2 files changed, 46 insertions, 1 deletions
@@ -1,3 +1,3 @@ -# Miguel's Haskell Collections +# Miguel's Haskell Collection A collection of some small haskell sources of mine. diff --git a/base64/base64.hs b/base64/base64.hs new file mode 100644 index 0000000..13f12bb --- /dev/null +++ b/base64/base64.hs @@ -0,0 +1,45 @@ +-- +-- Miguel's Naive +-- Base64 Encoder -- Coded on a winter afterfnoon on 19th Feb 2018 A.D. +-- to fully understand base64 encoding and play with +-- haskell, which is always an indisputable pleasure. +-- +-- The following lines were written in full awareness +-- that 'libraries' for this purpose, which perform way +-- better, are in existence. +-- +-- Coded in big anger due to nick's stories about saving his binary +-- format, encrypted passwords in an ascii config file, featuring +-- strange letters and characters. +-- +-- Example Usage: echo "just testing" | stack runghc base64.hs | stack runghc base64 -- -d +-- (You can cross check with base64 / base64 -d) + +import System.Environment +import qualified Data.List.Split as T +import qualified Data.List as L +import qualified Data.Map.Strict as M +import qualified Data.ByteString.Lazy as B +import Text.Printf +import Data.Maybe +import Data.Char +import Control.Monad + +main = do args<-getArgs + if length args == 0 then go encode64 + else when (args!!0 == "-d") $ go decode64 + where go f = getContents >>= putStr.(++"\n").f + +fromBase64 x = (['A'..'Z']++['a'..'z']++['0'..'9']++['+','/']) !! x +mapBase64 = M.fromList $ map (\x-> (fromBase64 x,x)) [0..63] +binToDec = sum . map (2^) . L.findIndices (=='1') . reverse + +encode64 x = pad64 (length x) . map (fromBase64 . binToDec) . T.chunksOf 6 . + concat . map (printf "%08b" ) $ x ++ cycle "\000" + +decode64 x = map (chr . binToDec) . T.chunksOf 8 . + concat . map ( printf "%06b" . fromJust) . filter isJust . + map (flip M.lookup mapBase64) $ x + +pad64 l = (++(take m ((cycle "=")))) . take ((l+m)*3 `div` 2 -3-m) + where m = mod (3 - mod l 3) 3 |
