base64

Fast Haskell base64 encoding/decoding (docs.ppad.tech/base64).
git clone git://git.ppad.tech/base64.git
Log | Files | Refs | README | LICENSE

Base64.hs (1337B)


      1 {-# OPTIONS_HADDOCK prune #-}
      2 
      3 -- |
      4 -- Module: Data.ByteString.Base64
      5 -- Copyright: (c) 2026 Jared Tobin
      6 -- License: MIT
      7 -- Maintainer: Jared Tobin <jared@ppad.tech>
      8 --
      9 -- Base64 encoding and decoding of strict bytestrings.
     10 
     11 module Data.ByteString.Base64 (
     12     encode
     13   , decode
     14   ) where
     15 
     16 import qualified Data.ByteString as BS
     17 import qualified Data.ByteString.Base64.Arm as Arm
     18 import qualified Data.ByteString.Base64.Pure as Pure
     19 
     20 -- | Encode a base256 'ByteString' as base64.
     21 --
     22 --   Uses ARM NEON extensions when available, otherwise a pure
     23 --   Haskell scalar loop.
     24 --
     25 --   >>> encode "hello world"
     26 --   "aGVsbG8gd29ybGQ="
     27 encode :: BS.ByteString -> BS.ByteString
     28 encode bs
     29   | Arm.base64_arm_available = Arm.encode bs
     30   | otherwise                = Pure.encode bs
     31 {-# INLINABLE encode #-}
     32 
     33 -- | Decode a base64 'ByteString' to base256.
     34 --
     35 --   Uses ARM NEON extensions when available, otherwise a pure
     36 --   Haskell scalar loop.  Invalid inputs (including incorrectly-
     37 --   padded or non-canonical inputs) will produce 'Nothing'.
     38 --
     39 --   >>> decode "aGVsbG8gd29ybGQ="
     40 --   Just "hello world"
     41 --   >>> decode "aGVsbG8gd29ybGQ" -- missing padding
     42 --   Nothing
     43 decode :: BS.ByteString -> Maybe BS.ByteString
     44 decode bs
     45   | Arm.base64_arm_available = Arm.decode bs
     46   | otherwise                = Pure.decode bs
     47 {-# INLINABLE decode #-}