base (ex_stdlib v0.3.0)
View SourceBase 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;falseto omit it when encoding and make it optional when decoding.case-upper(default for encoding),lower, ormixed(decoding only) for base16 and base32.letter_caseis accepted as an alias. Base16 and base32 decoding default tomixed.ignore-whitespaceto 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
Types
Kept for backwards compatibility.
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
-type decode_case() :: upper | lower | mixed.
-type decode_options() :: options().
-type encode_case() :: upper | lower.
-type option() :: {padding, padding()} | {'case', decode_case()} | {letter_case, decode_case()} | {ignore, whitespace | none}.
-type options() :: #{padding => padding(), 'case' => decode_case(), letter_case => decode_case(), ignore => whitespace | none} | [option()].
Kept for backwards compatibility.
-type padding() :: boolean().
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.