View Source Base (Elixir v1.14.2)

This module provides data encoding and decoding functions according to RFC 4648.

This document defines the commonly used base 16, base 32, and base 64 encoding schemes.

Base 16 alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
00448812C
11559913D
226610A14E
337711B15F

Base 32 alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
0A9J18S273
1B10K19T284
2C11L20U295
3D12M21V306
4E13N22W317
5F14O23X
6G15P24Y(pad)=
7H16Q25Z
8I17R262

Base 32 (extended hex) alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
009918I27R
1110A19J28S
2211B20K29T
3312C21L30U
4413D22M31V
5514E23N
6615F24O(pad)=
7716G25P
8817H26Q

Base 64 alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
0A17R34i51z
1B18S35j520
2C19T36k531
3D20U37l542
4E21V38m553
5F22W39n564
6G23X40o575
7H24Y41p586
8I25Z42q597
9J26a43r608
10K27b44s619
11L28c45t62+
12M29d46u63/
13N30e47v
14O31f48w(pad)=
15P32g49x
16Q33h50y

Base 64 (URL and filename safe) alphabet

ValueEncodingValueEncodingValueEncodingValueEncoding
0A17R34i51z
1B18S35j520
2C19T36k531
3D20U37l542
4E21V38m553
5F22W39n564
6G23X40o575
7H24Y41p586
8I25Z42q597
9J26a43r608
10K27b44s619
11L28c45t62-
12M29d46u63_
13N30e47v
14O31f48w(pad)=
15P32g49x
16Q33h50y

Link to this section Summary

Functions

Decodes a base 16 encoded string into a binary string.

Decodes a base 16 encoded string into a binary string.

Decodes a base 32 encoded string into a binary string.

Decodes a base 32 encoded string into a binary string.

Decodes a base 64 encoded string into a binary string.

Decodes a base 64 encoded string into a binary string.

Encodes a binary string into a base 16 encoded string.

Encodes a binary string into a base 32 encoded string.

Encodes a binary string into a base 64 encoded string.

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

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

Encodes a binary string into a base 32 encoded string with an extended hexadecimal alphabet.

Decodes a base 64 encoded string with URL and filename safe alphabet into a binary string.

Decodes a base 64 encoded string with URL and filename safe alphabet into a binary string.

Encodes a binary string into a base 64 encoded string with URL and filename safe alphabet.

Link to this section Types

@type decode_case() :: :upper | :lower | :mixed
@type encode_case() :: :upper | :lower

Link to this section Functions

Link to this function

decode16(string, opts \\ [])

View Source
@spec decode16(binary(), [{:case, decode_case()}]) :: {:ok, binary()} | :error

Decodes a base 16 encoded string into a binary string.

Options

The accepted options are:

  • :case - specifies the character case to accept when decoding

The values for :case can be:

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

Examples

iex> Base.decode16("666F6F626172")
{:ok, "foobar"}

iex> Base.decode16("666f6f626172", case: :lower)
{:ok, "foobar"}

iex> Base.decode16("666f6F626172", case: :mixed)
{:ok, "foobar"}
Link to this function

decode16!(string, opts \\ [])

View Source
@spec decode16!(binary(), [{:case, decode_case()}]) :: binary()

Decodes a base 16 encoded string into a binary string.

Options

The accepted options are:

  • :case - specifies the character case to accept when decoding

The values for :case can be:

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

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

Examples

iex> Base.decode16!("666F6F626172")
"foobar"

iex> Base.decode16!("666f6f626172", case: :lower)
"foobar"

iex> Base.decode16!("666f6F626172", case: :mixed)
"foobar"
Link to this function

decode32(string, opts \\ [])

View Source
@spec decode32(binary(), case: decode_case(), padding: boolean()) ::
  {:ok, binary()} | :error

Decodes a base 32 encoded string 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 (default)
  • false - ignores padding from the input string

Examples

iex> Base.decode32("MZXW6YTBOI======")
{:ok, "foobar"}

iex> Base.decode32("mzxw6ytboi======", case: :lower)
{:ok, "foobar"}

iex> Base.decode32("mzXW6ytBOi======", case: :mixed)
{:ok, "foobar"}

iex> Base.decode32("MZXW6YTBOI", padding: false)
{:ok, "foobar"}
Link to this function

decode32!(string, opts \\ [])

View Source
@spec decode32!(binary(), case: decode_case(), padding: boolean()) :: binary()

Decodes a base 32 encoded string 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> Base.decode32!("MZXW6YTBOI======")
"foobar"

iex> Base.decode32!("mzxw6ytboi======", case: :lower)
"foobar"

iex> Base.decode32!("mzXW6ytBOi======", case: :mixed)
"foobar"

iex> Base.decode32!("MZXW6YTBOI", padding: false)
"foobar"
Link to this function

decode64(string, opts \\ [])

View Source
@spec decode64(binary(), ignore: :whitespace, padding: boolean()) ::
  {:ok, binary()} | :error

Decodes a base 64 encoded string into a binary string.

Accepts ignore: :whitespace option which will ignore all the whitespace characters in the input string.

Accepts padding: false option which will ignore padding from the input string.

Examples

iex> Base.decode64("Zm9vYmFy")
{:ok, "foobar"}

iex> Base.decode64("Zm9vYmFy\n", ignore: :whitespace)
{:ok, "foobar"}

iex> Base.decode64("Zm9vYg==")
{:ok, "foob"}

iex> Base.decode64("Zm9vYg", padding: false)
{:ok, "foob"}
Link to this function

decode64!(string, opts \\ [])

View Source
@spec decode64!(binary(), ignore: :whitespace, padding: boolean()) :: binary()

Decodes a base 64 encoded string into a binary string.

Accepts ignore: :whitespace option which will ignore all the whitespace characters in the input string.

Accepts padding: false option which will ignore padding from the input string.

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

Examples

iex> Base.decode64!("Zm9vYmFy")
"foobar"

iex> Base.decode64!("Zm9vYmFy\n", ignore: :whitespace)
"foobar"

iex> Base.decode64!("Zm9vYg==")
"foob"

iex> Base.decode64!("Zm9vYg", padding: false)
"foob"
Link to this function

encode16(data, opts \\ [])

View Source
@spec encode16(binary(), [{:case, encode_case()}]) :: binary()

Encodes a binary string into a base 16 encoded string.

Options

The accepted options are:

  • :case - specifies the character case to use when encoding

The values for :case can be:

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

Examples

iex> Base.encode16("foobar")
"666F6F626172"

iex> Base.encode16("foobar", case: :lower)
"666f6f626172"
Link to this function

encode32(data, opts \\ [])

View Source
@spec encode32(binary(), case: encode_case(), padding: boolean()) :: binary()

Encodes a binary string into a base 32 encoded string.

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 (default)
  • false - omit padding from the output string

Examples

iex> Base.encode32("foobar")
"MZXW6YTBOI======"

iex> Base.encode32("foobar", case: :lower)
"mzxw6ytboi======"

iex> Base.encode32("foobar", padding: false)
"MZXW6YTBOI"
Link to this function

encode64(data, opts \\ [])

View Source
@spec encode64(binary(), [{:padding, boolean()}]) :: binary()

Encodes a binary string into a base 64 encoded string.

Accepts padding: false option which will omit padding from the output string.

Examples

iex> Base.encode64("foobar")
"Zm9vYmFy"

iex> Base.encode64("foob")
"Zm9vYg=="

iex> Base.encode64("foob", padding: false)
"Zm9vYg"
Link to this function

hex_decode32(string, opts \\ [])

View Source
@spec hex_decode32(binary(), case: decode_case(), padding: boolean()) ::
  {:ok, binary()} | :error

Decodes a base 32 encoded string with extended hexadecimal 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 (default)
  • false - ignores padding from the input string

Examples

iex> Base.hex_decode32("CPNMUOJ1E8======")
{:ok, "foobar"}

iex> Base.hex_decode32("cpnmuoj1e8======", case: :lower)
{:ok, "foobar"}

iex> Base.hex_decode32("cpnMuOJ1E8======", case: :mixed)
{:ok, "foobar"}

iex> Base.hex_decode32("CPNMUOJ1E8", padding: false)
{:ok, "foobar"}
Link to this function

hex_decode32!(string, opts \\ [])

View Source
@spec hex_decode32!(binary(), case: decode_case(), padding: boolean()) :: binary()

Decodes a base 32 encoded string with extended hexadecimal 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> Base.hex_decode32!("CPNMUOJ1E8======")
"foobar"

iex> Base.hex_decode32!("cpnmuoj1e8======", case: :lower)
"foobar"

iex> Base.hex_decode32!("cpnMuOJ1E8======", case: :mixed)
"foobar"

iex> Base.hex_decode32!("CPNMUOJ1E8", padding: false)
"foobar"
Link to this function

hex_encode32(data, opts \\ [])

View Source
@spec hex_encode32(binary(), case: encode_case(), padding: boolean()) :: binary()

Encodes a binary string into a base 32 encoded string with an extended hexadecimal 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 (default)
  • false - omit padding from the output string

Examples

iex> Base.hex_encode32("foobar")
"CPNMUOJ1E8======"

iex> Base.hex_encode32("foobar", case: :lower)
"cpnmuoj1e8======"

iex> Base.hex_encode32("foobar", padding: false)
"CPNMUOJ1E8"
Link to this function

url_decode64(string, opts \\ [])

View Source
@spec url_decode64(binary(), ignore: :whitespace, padding: boolean()) ::
  {:ok, binary()} | :error

Decodes a base 64 encoded string with URL and filename safe alphabet into a binary string.

Accepts ignore: :whitespace option which will ignore all the whitespace characters in the input string.

Accepts padding: false option which will ignore padding from the input string.

Examples

iex> Base.url_decode64("_3_-_A==")
{:ok, <<255, 127, 254, 252>>}

iex> Base.url_decode64("_3_-_A==\n", ignore: :whitespace)
{:ok, <<255, 127, 254, 252>>}

iex> Base.url_decode64("_3_-_A", padding: false)
{:ok, <<255, 127, 254, 252>>}
Link to this function

url_decode64!(string, opts \\ [])

View Source
@spec url_decode64!(binary(), ignore: :whitespace, padding: boolean()) :: binary()

Decodes a base 64 encoded string with URL and filename safe alphabet into a binary string.

Accepts ignore: :whitespace option which will ignore all the whitespace characters in the input string.

Accepts padding: false option which will ignore padding from the input string.

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

Examples

iex> Base.url_decode64!("_3_-_A==")
<<255, 127, 254, 252>>

iex> Base.url_decode64!("_3_-_A==\n", ignore: :whitespace)
<<255, 127, 254, 252>>

iex> Base.url_decode64!("_3_-_A", padding: false)
<<255, 127, 254, 252>>
Link to this function

url_encode64(data, opts \\ [])

View Source
@spec url_encode64(binary(), [{:padding, boolean()}]) :: binary()

Encodes a binary string into a base 64 encoded string with URL and filename safe alphabet.

Accepts padding: false option which will omit padding from the output string.

Examples

iex> Base.url_encode64(<<255, 127, 254, 252>>)
"_3_-_A=="

iex> Base.url_encode64(<<255, 127, 254, 252>>, padding: false)
"_3_-_A"