sandi-0.3.0.1: Data encoding library

Copyright(c) 2012 Magnus Therning
LicenseBSD3
Safe HaskellNone
LanguageHaskell98

Codec.Binary.Base64

Description

Implemented as specified in RFC 4648 (http://tools.ietf.org/html/rfc4648).

Base64 encoding works by expanding blocks of 3 bytes of data into blocks of 4 bytes of data. Finally it also includes a well defined ending of the encoded data to make sure the size of the final block of encoded data is 4 bytes too.

Synopsis

Documentation

b64_encode_part :: ByteString -> (ByteString, ByteString) Source

Encoding function.

This function encodes as large a portion of the input as possible and returns the encoded part together with the remaining part. Enough space is allocated for the encoding to make sure that the remaining part is less than 3 bytes long, which means it can be passed to b64_encode_final as is.

>>> b64_encode_part $ Data.ByteString.Char8.pack "foo"
("Zm9v","")
>>> b64_encode_part $ Data.ByteString.Char8.pack "foob"
("Zm9v","b")

b64_encode_final :: ByteString -> Maybe ByteString Source

Encoding function for the final block.

The final block has to have a size less than 3.

>>> b64_encode_final $ Data.ByteString.Char8.pack "r"
Just "cg=="

Trying to pass in too large a block result in failure:

>>> b64_encode_final $ Data.ByteString.Char8.pack "foo"
Nothing

b64_decode_part :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString) Source

Decoding function.

Decode as large a portion of the input as possible. Enough data is allocated for the output to ensure that the remainder is less than 4 bytes in size. Success result in a Right value:

>>> b64_decode_part $ Data.ByteString.Char8.pack "Zm9v"
Right ("foo","")
>>> b64_decode_part $ Data.ByteString.Char8.pack "Zm9vYmE="
Right ("foo","YmE=")

Failures occur on bad input and result in a Left value:

>>> b64_decode_part $ Data.ByteString.Char8.pack "Z=9v"
Left ("","Z=9v")

b64_decode_final :: ByteString -> Maybe ByteString Source

Decoding function for the final block.

The final block has to have a size of 0 or 4:

>>> b64_decode_final $ Data.ByteString.Char8.pack "Zm8="
Just "fo"
>>> b64_decode_final $ Data.ByteString.Char8.pack ""
Just ""
>>> b64_decode_final $ Data.ByteString.Char8.pack "Zm="
Nothing

But it must be the encoding of a block that is less than 3 bytes:

>>> b64_decode_final $ encode $ Data.ByteString.Char8.pack "foo"
Nothing

encode :: ByteString -> ByteString Source

Convenience function that combines b64_encode_part and b64_encode_final to encode a complete string.

>>> encode $ Data.ByteString.Char8.pack "foo"
"Zm9v"
>>> encode $ Data.ByteString.Char8.pack "foobar"
"Zm9vYmFy"

decode :: ByteString -> Either (ByteString, ByteString) ByteString Source

Convenience function that combines b64_decode_part and b64_decode_final to decode a complete string.

>>> decode $ Data.ByteString.Char8.pack "Zm9v"
Right "foo"
>>> decode $ Data.ByteString.Char8.pack "Zm9vYmFy"
Right "foobar"

Failures when decoding returns the decoded part and the remainder:

>>> decode $ Data.ByteString.Char8.pack "Zm9vYm=y"
Left ("foo","Ym=y")