Base64

This library implements the Base64 encoding algorithm as defined in RFC 4648.

Usage

Add "base64" to the dependencies listed in your project’s “dylan-package.json” file and run deft update to install the base64 package and update your workspace registry. Add use base64; to your library and module definitions.

The base64 library exports two functions:

Both of these function accept a scheme: keyword argument of type <scheme> which may be one of the following constants:

  • $standard-scheme - the standard base64 character set including + and

    /.

  • $url-scheme - a character set safe for use in URLs and filenames, in which + is replaced by - and / is replaced by _.

Note

There is currently no support for line breaks or whitespace in the input/output, nor for base64 streams. Pull requests welcome.

The base64 Module

<scheme> Type

Equivalent to one-of($standard-scheme, $url-scheme).

$standard-scheme Constant

An instance of <scheme> indicating to use the standard base64 encoding character set.

$url-scheme Constant

An instance of <scheme> indicating to use the URL and filename safe base64 character set.

base64-encode Function

Encode a byte sequence as a base 64 byte string as defined by RFC 4648.

Signature:

base64-encode (bytes, #key scheme, pad?) => (byte-string)

Parameters:
  • bytes – An instance of <sequence>. An error is signaled if the elements of this sequence are not either integers in the range 0 - 255 or byte characters.

  • scheme (#key) – An instance of <scheme>. May be either $standard-scheme (the default) or $url-scheme.

  • pad? (#key) – An instance of <boolean>. If true (the default) the returned byte string is padded with “=” to a multiple of 4 characters in length. This results in 0, 1, or 2 “=” characters at the end of the string.

Values:
Example:
base64-encode("foo") => "Zm9v"
base64-encode(#(251, 252, 253, 254, 255)) => "+/z9/v8="
base64-encode(#(251, 252, 253, 254, 255), scheme: $url-scheme) => "-_z9_v8="
base64-decode Function

Decode a base 64 encoded string into a byte sequence as defined by RFC 4648.

Signature:

base64-decode (string, #key scheme) => (bytes)

Parameters:
Values:
  • bytes – An instance of <byte-vector>.

Example:

Discussion:

Padding characters (“=”) at the end of the input string are automatically detected and ignored.

Example:
base64-decode("Zm8=") => {<simple-byte-vector>: 102, 111}
base64-decode("Zm8") => {<simple-byte-vector>: 102, 111}