memory-0.17.0: memory and related abstraction stuff
LicenseBSD-style
MaintainerVincent Hanquez <vincent@snarc.org>
Stabilityexperimental
Portabilityunknown
Safe HaskellNone
LanguageHaskell2010

Data.ByteArray.Encoding

Description

Base conversions for ByteArray.

Synopsis

Documentation

convertToBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> bout Source #

Encode some bytes to the equivalent representation in a specific Base.

Examples

Convert a ByteString to base-64:

>>> convertToBase Base64 ("foobar" :: ByteString) :: ByteString
"Zm9vYmFy"

convertFromBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> Either String bout Source #

Try to decode some bytes from the equivalent representation in a specific Base.

Examples

Successfully convert from base-64 to a ByteString:

>>> convertFromBase Base64 ("Zm9vYmFy" :: ByteString) :: Either String ByteString
Right "foobar"

Trying to decode invalid data will return an error string:

>>> convertFromBase Base64 ("!!!" :: ByteString) :: Either String ByteString
Left "base64: input: invalid length"

data Base Source #

The different bases that can be used.

See RFC4648 for details. In particular, Base64 can be standard or URL-safe. URL-safe encoding is often used in other specifications without padding characters.

RFC 2045 defines a separate Base64 encoding, which is not supported. This format requires a newline at least every 76 encoded characters, which works around limitations of older email programs that could not handle long lines. Be aware that other languages, such as Ruby, encode the RFC 2045 version by default. To decode their ouput, remove all newlines before decoding.

Examples

A quick example to show the differences:

>>> let input = "Is 3 > 2?" :: ByteString
>>> let convertedTo base = convertToBase base input :: ByteString
>>> convertedTo Base16
"49732033203e20323f"
>>> convertedTo Base32
"JFZSAMZAHYQDEPY="
>>> convertedTo Base64
"SXMgMyA+IDI/"
>>> convertedTo Base64URLUnpadded
"SXMgMyA-IDI_"
>>> convertedTo Base64OpenBSD
"QVKeKw.8GBG9"

Constructors

Base16

similar to hexadecimal

Base32 
Base64

standard Base64

Base64URLUnpadded

unpadded URL-safe Base64

Base64OpenBSD

Base64 as used in OpenBSD password encoding (such as bcrypt)

Instances

Instances details
Eq Base Source # 
Instance details

Defined in Data.ByteArray.Encoding

Methods

(==) :: Base -> Base -> Bool #

(/=) :: Base -> Base -> Bool #

Show Base Source # 
Instance details

Defined in Data.ByteArray.Encoding

Methods

showsPrec :: Int -> Base -> ShowS #

show :: Base -> String #

showList :: [Base] -> ShowS #