base (ex_stdlib v0.2.0)

View Source

Base encoding and decoding module.

This module provides functions for encoding and decoding data using various base encoding schemes: base16 (hex), base32, base64, and their URL-safe variants.

Examples:

   Encoded = base:encode64("Hello, World!"),
   Decoded = base:decode64(Encoded),
   HexEncoded = base:encode16("binary data"),
   UrlSafe = base:url_encode64("data with + and /").

Summary

Functions

Decodes Base16 (hexadecimal) encoded data.

Decodes Base16 (hexadecimal) encoded data with options.

Decodes standard Base32-encoded data.

Decodes standard Base32-encoded data with options.

Decodes Base64-encoded data.

Decodes Base64-encoded data with options.

Encodes binary data using Base16 (hexadecimal) encoding.

Encodes binary data using standard Base32 encoding.

Encodes binary data using Base64 encoding.

Decodes Base32 hex-encoded data.

Decodes Base32 hex-encoded data with options.

Encodes binary data using Base32 hex encoding.

Decodes URL-safe Base64-encoded data.

Decodes URL-safe Base64-encoded data with options.

Encodes binary data using URL-safe Base64 encoding.

Types

decode_options/0

-type decode_options() :: #{ignore => whitespace | none, letter_case => mixed | upper | lower}.

encode_case/0

-type encode_case() :: upper | lower.

padding/0

-type padding() :: true | false.

Functions

decode16(Data)

-spec decode16(binary() | string()) -> binary().

Decodes Base16 (hexadecimal) encoded data.

Accepts both uppercase and lowercase hexadecimal.

decode16(Data, Options)

-spec decode16(binary() | string(), decode_options()) -> {ok, binary()} | error.

Decodes Base16 (hexadecimal) encoded data with options.

Options: - letter_case: mixed | upper | lower - expected case of hex digits Returns {ok, Decoded} on success, or error on invalid input.

decode32(Data)

-spec decode32(binary() | string()) -> binary().

Decodes standard Base32-encoded data.

Returns the decoded binary data. Raises an error if the input is invalid.

decode32(Data, Options)

-spec decode32(binary() | string(), decode_options()) -> {ok, binary()} | error.

Decodes standard Base32-encoded data with options.

Returns {ok, Decoded} on success, or error on invalid input.

decode64(Data)

-spec decode64(binary() | string()) -> binary().

Decodes Base64-encoded data.

Returns the decoded binary data. Raises an error if the input is invalid.

decode64(Data, Options)

-spec decode64(binary() | string(), decode_options()) -> {ok, binary()} | error.

Decodes Base64-encoded data with options.

Options: - ignore: whitespace | none - whether to ignore whitespace characters Returns {ok, Decoded} on success, or error on invalid input.

encode16(Data)

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

Encodes binary data using Base16 (hexadecimal) encoding.

Returns uppercase hexadecimal by default.

encode32(Data)

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

Encodes binary data using standard Base32 encoding.

Uses the standard Base32 alphabet (A-Z, 2-7) with padding.

encode64(Data)

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

Encodes binary data using Base64 encoding.

Returns a binary containing the Base64-encoded data with padding.

hex_decode32(Data)

-spec hex_decode32(binary() | string()) -> binary().

Decodes Base32 hex-encoded data.

Returns the decoded binary data. Raises an error if the input is invalid.

hex_decode32(Data, Options)

-spec hex_decode32(binary() | string(), decode_options()) -> {ok, binary()} | error.

Decodes Base32 hex-encoded data with options.

Returns {ok, Decoded} on success, or error on invalid input.

hex_encode32(Data)

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

Encodes binary data using Base32 hex encoding.

Uses the extended hex alphabet (0-9, A-V) with padding.

url_decode64(Data)

-spec url_decode64(binary() | string()) -> binary().

Decodes URL-safe Base64-encoded data.

Handles both padded and unpadded URL-safe Base64.

url_decode64(Data, Options)

-spec url_decode64(binary() | string(), decode_options()) -> {ok, binary()} | error.

Decodes URL-safe Base64-encoded data with options.

Returns {ok, Decoded} on success, or error on invalid input.

url_encode64(Data)

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

Encodes binary data using URL-safe Base64 encoding.

URL-safe Base64 uses '-' and '_' instead of '+' and '/', and omits padding.