module System.Nix.Base32 where
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Data.Vector as V
encode :: BS.ByteString -> T.Text
encode c = T.pack $ concatMap char32 [nChar - 1, nChar - 2 .. 0]
where
digits32 = V.fromList "0123456789abcdfghijklmnpqrsvwxyz"
nChar = fromIntegral $ ((BS.length c * 8 - 1) `div` 5) + 1
char32 :: Integer -> [Char]
char32 i = [digits32 V.! digitInd]
where
byte j = BS.index c (fromIntegral j)
fromIntegral' :: Num b => Integer -> b
fromIntegral' = fromIntegral
digitInd = fromIntegral' $
sum [fromIntegral (byte j) * (256^j)
| j <- [0 .. BS.length c - 1]]
`div` (32^i)
`mod` 32