base64: RFC 4648-compliant padded and unpadded base64 and base64url encodings

[ bsd3, data, library ] [ Propose Tags ]

RFC 4648-compliant padded and unpadded base64 and base64url encoding and decoding.

[Skip to Readme]


Manual Flags


Enable optics for base64-encoding text and bytestrings


Performance tuning flags and information


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Versions [RSS],,,,,, 0.4.0, 0.4.1, 0.4.2,,,, (info)
Change log
Dependencies base (>=4.10 && <5), bytestring, deepseq, lens (>=4.18 && <4.19), text [details]
License BSD-3-Clause
Copyright (c) 2019 Emily Pillmore
Author Emily Pillmore
Category Data
Home page
Bug tracker
Source repo head: git clone
Uploaded by topos at 2020-01-05T23:11:46Z
Distributions Arch:, LTSHaskell:, NixOS:, Stackage:, openSUSE:
Downloads 13650 total (187 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for base64-

[back to package description]


Build Status Hackage

Padded and unpadded base64 and base64url encodings for Text and ByteString values, along with their optics.


What does this library provide? Here is the summary:

  • Better performance over existing Base64 libraries (2x and 3x for most use-cases - see
  • Support for unpadded Base64 and Base64-url
  • Support for Text encodings and decodings
  • Optics for handling more complex structures with Base64 representations


Haskell has two main libraries for Base64: memory, and base64-bytestring.

Of these, memory is geared towards integration with other memory primitives in the library, without much of an eye towards performance, while base64-bytestring is built to exclusively address ByteString encoding and decoding, and is very performant. Many great strides have been made in the realm of Base64 performance and vectorization just in the past 5 years, which this library attempts to capture. Additionally, we attempt to fix percieved shortcomings with both APIs in the support of unpadded Base64 and Base64-url support (which memory provides, but not base64-bytestring), as well as supporting Text values (neither libraries provide), supplying some optics for composing structures with Base64-encodable/decodable focii (neither libraries provide), and convenient pattern synonyms (no library to date does this).


The pattern synonyms provided in this library are:

pattern Base64 :: ByteString -> ByteString
pattern Base64Url :: ByteString -> ByteString
pattern Base64Unpadded :: ByteString -> ByteString
pattern Base64UrlUnpadded :: ByteString -> ByteString

-- and

pattern Base64 :: Text -> Text
pattern Base64Url :: Text -> Text
pattern Base64Unpadded :: Text -> Text
pattern Base64UrlUnpadded :: Text -> Text

These provide a convenient high level interface for passing Base64 encoded values.


Prisms for encoding and decoding Text and ByteString values are given as part of the library:

_Base64 :: Prism' ByteString ByteString
_Base64Url :: Prism' ByteString ByteString
_Base64Unpadded :: Prism' ByteString ByteString
_Base64UrlUnpadded :: Prism' ByteString ByteString

-- and

_Base64 :: Prism' Text Text
_Base64Url :: Prism' Text Text
_Base64Unpadded :: Prism' Text Text
_Base64UrlUnpadded :: Prism' Text Text

If a particular structure has a Lens into some Text or ByteString value they might want to encode (or decode), then composing such a Lens with these Prisms yields an affine Traversal, resulting in a structure which has the focus of its Lens encoded as or decoded from Base64(-url). All one needs to do is compose their optics:

data MyStruct = MyStruct
  { _a :: Int
  , _b :: Text
  } deriving Show

b :: Lens' MyStruct Text
b = lens _b (\t b_ -> t { _b = b_ })

myB64Struct :: Traversal' s Text
myB64Struct = b . _Base64

-- >>> MyStruct 3 "U3Vu" ^? b . _Base64
-- MyStruct {_a = 3, _b = "Sun"}

bRe :: Review MyStruct Text
bRe = unto (\b -> MyStruct 0 b)

-- >>> bRe . _Base64 # "Sun"
-- MyStruct {_a = 0, _b = "UV3u"}

The data of a Prism naturally conforms to this "encoding/decoding" dichotomy, where the Review, or "builder" half of the Prism of type b -> t is an encoding, and the "Matcher" half of the prism, of type s -> Either t a, represents a decoding of a similar structure. Hence, Prism is the most appropriate structure.