UXID.CrockfordBase32 (UXID v0.0.4) View Source

This module provides data encoding and decoding functions according to Crockford Base32

Crockford's Base 32 (UX focused - can be read over the phone) alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
009918J27V
1110A19K28W
2211B20M29X
3312C21N30Y
4413D22P31Z
5514E23Q
6615F24R(pad)=
7716G25S
8817H26T

Link to this section Summary

Functions

Decodes a base 32 encoded string with Crockford's alphabet into a binary string.

Decodes a base 32 encoded string with extended crockfordadecimal alphabet into a binary string.

Encodes a binary string into a base 32 encoded string using Crockford's alphabet.

Link to this section Functions

Link to this function

decode(string, opts \\ [])

View Source

Specs

decode(binary(), keyword()) :: {:ok, binary()} | :error

Decodes a base 32 encoded string with Crockford's alphabet into a binary string.

Options

The accepted options are:

  • :case - specifies the character case to accept when decoding
  • :padding - specifies whether to require padding

The values for :case can be:

  • :upper - only allows upper case characters (default)
  • :lower - only allows lower case characters
  • :mixed - allows mixed case characters

The values for :padding can be:

  • true - requires the input string to be padded to the nearest multiple of 8
  • false - ignores padding from the input string (default)

Examples

iex> UXID.CrockfordBase32.decode("CSQPYRK1E8")
{:ok, "foobar"}

iex> UXID.CrockfordBase32.decode("csqpyrk1e8", case: :lower)
{:ok, "foobar"}

iex> UXID.CrockfordBase32.decode("csqPyRK1E8", case: :mixed)
{:ok, "foobar"}

iex> UXID.CrockfordBase32.decode("CSQPYRK1E8======", padding: true)
{:ok, "foobar"}
Link to this function

decode!(string, opts \\ [])

View Source

Specs

decode!(binary(), keyword()) :: binary()

Decodes a base 32 encoded string with extended crockfordadecimal alphabet into a binary string.

An ArgumentError exception is raised if the padding is incorrect or a non-alphabet character is present in the string.

Options

The accepted options are:

  • :case - specifies the character case to accept when decoding
  • :padding - specifies whether to require padding

The values for :case can be:

  • :upper - only allows upper case characters (default)
  • :lower - only allows lower case characters
  • :mixed - allows mixed case characters

The values for :padding can be:

  • true - requires the input string to be padded to the nearest multiple of 8 (default)
  • false - ignores padding from the input string

Examples

iex> UXID.CrockfordBase32.decode!("CSQPYRK1E8")
"foobar"

iex> UXID.CrockfordBase32.decode!("csqpyrk1e8", case: :lower)
"foobar"

iex> UXID.CrockfordBase32.decode!("csqPyRK1E8", case: :mixed)
"foobar"

iex> UXID.CrockfordBase32.decode!("CSQPYRK1E8======", padding: true)
"foobar"
Link to this function

encode(data, opts \\ [])

View Source

Specs

encode(binary(), keyword()) :: binary()

Encodes a binary string into a base 32 encoded string using Crockford's alphabet.

Options

The accepted options are:

  • :case - specifies the character case to use when encoding
  • :padding - specifies whether to apply padding

The values for :case can be:

  • :upper - uses upper case characters (default)
  • :lower - uses lower case characters

The values for :padding can be:

  • true - pad the output string to the nearest multiple of 8
  • false - omit padding from the output string (default)

Examples

iex> UXID.CrockfordBase32.encode("foobar")
"CSQPYRK1E8"

iex> UXID.CrockfordBase32.encode("foobar", case: :lower)
"csqpyrk1e8"

iex> UXID.CrockfordBase32.encode("foobar", padding: true)
"CSQPYRK1E8======"