base (ex_stdlib v0.3.0)

View Source

Base encoding and decoding module, inspired by Elixir's Base module.

This module provides functions for encoding and decoding data using the encodings of RFC 4648: base16 (hex), base32, base32hex, base64 and URL-safe base64.

Examples:

   <<"Zm9vYmFy">> = base:encode64(<<"foobar">>),
   <<"foobar">> = base:decode64(<<"Zm9vYmFy">>),
   {ok, <<"foob">>} = base:decode64(<<"Zm9vYg">>, #{padding => false}),
   <<"666f6f">> = base:encode16(<<"foo">>, #{'case' => lower}).

Options

Options are given as a map or a property list:

  • padding - true (default) to add padding when encoding and require it when decoding; false to omit it when encoding and make it optional when decoding.
  • case - upper (default for encoding), lower, or mixed (decoding only) for base16 and base32. letter_case is accepted as an alias. Base16 and base32 decoding default to mixed.
  • ignore - whitespace to ignore whitespace when decoding; none (default) rejects it.

Return values

The one-argument decode functions return the decoded binary and raise {invalid_baseNN, Reason} on invalid input, like Elixir's decodeNN!. The two-argument versions return {ok, Binary} or error.

Summary

Functions

Decodes Base16 data, accepting both cases. Raises {invalid_base16, Reason} on invalid input.

Decodes Base16 data. Accepts the case (default mixed) and ignore options.

Decodes Base32 data, raising {invalid_base32, Reason} on invalid input.

Decodes Base32 data. Accepts the padding, case and ignore options.

Decodes Base64 data, raising {invalid_base64, Reason} on invalid input.

Decodes Base64 data. Accepts the padding and ignore options.

Encodes data using Base16 (hexadecimal), in uppercase.

Encodes data using Base16. Accepts the case option.

Encodes data using Base32 (A-Z, 2-7), with padding.

Encodes data using Base32. Accepts the padding and case options.

Encodes data using Base64, with padding.

Encodes data using Base64. Accepts the padding option.

Decodes Base32 hex data, raising {invalid_base32, Reason} on invalid input.

Decodes Base32 hex data. Accepts the padding, case and ignore options.

Encodes data using Base32 with the extended hex alphabet (0-9, A-V), with padding.

Encodes data using Base32 hex. Accepts the padding and case options.

Checks whether the data is valid Base32 hex.

Checks whether the data is valid Base32 hex, with options.

Decodes URL-safe Base64 data, raising {invalid_base64, Reason} on invalid input.

Decodes URL-safe Base64 data. Accepts the padding and ignore options.

Encodes data using URL and filename safe Base64 (- and _ instead of + and /), with padding. Pass #{padding => false} to omit it.

Encodes data using URL-safe Base64. Accepts the padding option.

Checks whether the data is valid URL-safe Base64.

Checks whether the data is valid URL-safe Base64, with options.

Checks whether the data is valid Base16.

Checks whether the data is valid Base16, with options.

Checks whether the data is valid Base32.

Checks whether the data is valid Base32, with options.

Checks whether the data is valid Base64.

Checks whether the data is valid Base64, with the same options as decode64/2.

Types

decode_case/0

-type decode_case() :: upper | lower | mixed.

decode_options/0

-type decode_options() :: options().

encode_case/0

-type encode_case() :: upper | lower.

option/0

-type option() ::
          {padding, padding()} |
          {'case', decode_case()} |
          {letter_case, decode_case()} |
          {ignore, whitespace | none}.

options/0

-type options() ::
          #{padding => padding(),
            'case' => decode_case(),
            letter_case => decode_case(),
            ignore => whitespace | none} |
          [option()].

Kept for backwards compatibility.

padding/0

-type padding() :: boolean().

Functions

decode16(Data)

-spec decode16(iodata()) -> binary().

Decodes Base16 data, accepting both cases. Raises {invalid_base16, Reason} on invalid input.

decode16(Data, Opts0)

-spec decode16(iodata(), options()) -> {ok, binary()} | error.

Decodes Base16 data. Accepts the case (default mixed) and ignore options.

decode32(Data)

-spec decode32(iodata()) -> binary().

Decodes Base32 data, raising {invalid_base32, Reason} on invalid input.

decode32(Data, Opts)

-spec decode32(iodata(), options()) -> {ok, binary()} | error.

Decodes Base32 data. Accepts the padding, case and ignore options.

decode64(Data)

-spec decode64(iodata()) -> binary().

Decodes Base64 data, raising {invalid_base64, Reason} on invalid input.

decode64(Data, Opts)

-spec decode64(iodata(), options()) -> {ok, binary()} | error.

Decodes Base64 data. Accepts the padding and ignore options.

encode16(Data)

-spec encode16(iodata()) -> binary().

Encodes data using Base16 (hexadecimal), in uppercase.

encode16(Data, Opts)

-spec encode16(iodata(), options()) -> binary().

Encodes data using Base16. Accepts the case option.

encode32(Data)

-spec encode32(iodata()) -> binary().

Encodes data using Base32 (A-Z, 2-7), with padding.

encode32(Data, Opts)

-spec encode32(iodata(), options()) -> binary().

Encodes data using Base32. Accepts the padding and case options.

encode64(Data)

-spec encode64(iodata()) -> binary().

Encodes data using Base64, with padding.

encode64(Data, Opts)

-spec encode64(iodata(), options()) -> binary().

Encodes data using Base64. Accepts the padding option.

hex_decode32(Data)

-spec hex_decode32(iodata()) -> binary().

Decodes Base32 hex data, raising {invalid_base32, Reason} on invalid input.

hex_decode32(Data, Opts)

-spec hex_decode32(iodata(), options()) -> {ok, binary()} | error.

Decodes Base32 hex data. Accepts the padding, case and ignore options.

hex_encode32(Data)

-spec hex_encode32(iodata()) -> binary().

Encodes data using Base32 with the extended hex alphabet (0-9, A-V), with padding.

hex_encode32(Data, Opts)

-spec hex_encode32(iodata(), options()) -> binary().

Encodes data using Base32 hex. Accepts the padding and case options.

hex_valid32(Data)

-spec hex_valid32(iodata()) -> boolean().

Checks whether the data is valid Base32 hex.

hex_valid32(Data, Opts)

-spec hex_valid32(iodata(), options()) -> boolean().

Checks whether the data is valid Base32 hex, with options.

url_decode64(Data)

-spec url_decode64(iodata()) -> binary().

Decodes URL-safe Base64 data, raising {invalid_base64, Reason} on invalid input.

url_decode64(Data, Opts)

-spec url_decode64(iodata(), options()) -> {ok, binary()} | error.

Decodes URL-safe Base64 data. Accepts the padding and ignore options.

url_encode64(Data)

-spec url_encode64(iodata()) -> binary().

Encodes data using URL and filename safe Base64 (- and _ instead of + and /), with padding. Pass #{padding => false} to omit it.

url_encode64(Data, Opts)

-spec url_encode64(iodata(), options()) -> binary().

Encodes data using URL-safe Base64. Accepts the padding option.

url_valid64(Data)

-spec url_valid64(iodata()) -> boolean().

Checks whether the data is valid URL-safe Base64.

url_valid64(Data, Opts)

-spec url_valid64(iodata(), options()) -> boolean().

Checks whether the data is valid URL-safe Base64, with options.

valid16(Data)

-spec valid16(iodata()) -> boolean().

Checks whether the data is valid Base16.

valid16(Data, Opts)

-spec valid16(iodata(), options()) -> boolean().

Checks whether the data is valid Base16, with options.

valid32(Data)

-spec valid32(iodata()) -> boolean().

Checks whether the data is valid Base32.

valid32(Data, Opts)

-spec valid32(iodata(), options()) -> boolean().

Checks whether the data is valid Base32, with options.

valid64(Data)

-spec valid64(iodata()) -> boolean().

Checks whether the data is valid Base64.

valid64(Data, Opts)

-spec valid64(iodata(), options()) -> boolean().

Checks whether the data is valid Base64, with the same options as decode64/2.