Module base64

An implementation of a subset of the Erlang/OTP base64 interface.

Description

This module is designed to be API-compatible with the Erlang/OTP base64 module, with the following exceptions:

  • No support for decoding data with whitespace in base64 data

  • No support for mime decoding functions

Data Types

decode_options()


decode_options() = #{padding => boolean(), mode => standard | urlsafe}

Options for decoding functions.

  • padding: when true (default), the input must be padded with the correct number of = characters. When false, missing = padding is accepted, though padded input is still accepted too.

  • mode: selects the decoding alphabet.standard (default) uses the standard alphabet from RFC 4648 Section 4; + and / are valid, while - and _ are illegal.urlsafe uses the URL and filename safe alphabet from RFC 4648 Section 5; - and _ are valid, while + and / are illegal.

encode_options()


encode_options() = #{padding => boolean(), mode => standard | urlsafe}

Options for encoding functions.

  • padding: when true (default), appends = padding characters to the encoded output so its length is a multiple of 4. When false, trailing = characters are omitted.

  • mode: selects the encoding alphabet.standard (default) uses the standard alphabet from RFC 4648 Section 4 (+ and / for indices 62 and 63).urlsafe uses the URL and filename safe alphabet from RFC 4648 Section 5 (- and _ for indices 62 and 63).

Function Index

decode/1 Equivalent to decode(Data, #{}).
decode/2 Base-64 decode a binary or string, returning a binary.
decode_to_string/1 Equivalent to decode_to_string(Data, #{}).
decode_to_string/2 Base-64 decode a binary or string, returning a string.
encode/1 Equivalent to encode(Data, #{}).
encode/2 Base-64 encode a binary or string, returning a binary.
encode_to_string/1 Equivalent to encode_to_string(Data, #{}).
encode_to_string/2 Base-64 encode a binary or string, returning a string.

Function Details

decode/1


decode(Data::binary() | iolist()) -> binary()

Data: the base-64 encoded data to decode

returns: the decoded data as a binary

Equivalent to decode(Data, #{}).

Raises a badarg exception if the input is not valid base-64-encoded data. Whitespace in the input is not accepted.

decode/2


decode(Data::binary() | iolist(), Options::decode_options()) -> binary()

Data: the base-64 encoded data to decode
Options: decoding options

returns: the decoded data as a binary

Base-64 decode a binary or string, returning a binary.

When #{padding => true} (default), the input must carry the correct number of = padding characters. When #{padding => false}, missing = padding is tolerated; padded input is still accepted.

The mode option selects the decoding alphabet: standard (default, RFC 4648 Section 4) or urlsafe (RFC 4648 Section 5). Characters from the wrong alphabet cause a badarg exception.

Raises a badarg exception if the input is not valid base-64-encoded data. Whitespace in the input is not accepted.

decode_to_string/1


decode_to_string(Data::binary() | iolist()) -> string()

Data: the base-64 encoded data to decode

returns: the decoded data as a string

Equivalent to decode_to_string(Data, #{}).

Raises a badarg exception if the input is not valid base-64-encoded data. Whitespace in the input is not accepted.

decode_to_string/2


decode_to_string(Data::binary() | iolist(), Options::decode_options()) -> string()

Data: the base-64 encoded data to decode
Options: decoding options

returns: the decoded data as a string

Base-64 decode a binary or string, returning a string.

When #{padding => true} (default), the input must carry the correct number of = padding characters. When #{padding => false}, missing = padding is tolerated; padded input is still accepted.

The mode option selects the decoding alphabet: standard (default, RFC 4648 Section 4) or urlsafe (RFC 4648 Section 5). Characters from the wrong alphabet cause a badarg exception.

Raises a badarg exception if the input is not valid base-64-encoded data. Whitespace in the input is not accepted.

encode/1


encode(Data::binary() | iolist()) -> binary()

Data: the data to encode

returns: the base-64 encoded data as a binary

Equivalent to encode(Data, #{}).

encode/2


encode(Data::binary() | iolist(), Options::encode_options()) -> binary()

Data: the data to encode
Options: encoding options

returns: the base-64 encoded data as a binary

Base-64 encode a binary or string, returning a binary.

The padding option controls whether = padding characters are appended. The default is #{padding => true}.

The mode option selects the encoding alphabet: standard (default, RFC 4648 Section 4) or urlsafe (RFC 4648 Section 5).

encode_to_string/1


encode_to_string(Data::binary() | iolist()) -> string()

Data: the data to encode

returns: the base-64 encoded data as a string

Equivalent to encode_to_string(Data, #{}).

encode_to_string/2


encode_to_string(Data::binary() | iolist(), Options::encode_options()) -> string()

Data: the data to encode
Options: encoding options

returns: the base-64 encoded data as a string

Base-64 encode a binary or string, returning a string.

The padding option controls whether = padding characters are appended. The default is #{padding => true}.

The mode option selects the encoding alphabet: standard (default, RFC 4648 Section 4) or urlsafe (RFC 4648 Section 5).