The Base64 structure
Author: John Reppy
Keywords: Standard ML, SML, SML/NJ Library

The SML of NJ Library
The `Base64` structure

The `Base64` structure provides support for Base-64 encoding/decoding as specified by RFC 4648.

## Synopsis

    signature BASE64
    structure Base64 : BASE64

## Interface

    val isBase64 : char -> bool

    val encode : Word8Vector.vector -> string
    val encodeSlice : Word8VectorSlice.slice -> string

    exception Incomplete

    exception Invalid of (int * char)

    val decode : string -> Word8Vector.vector
    val decodeSlice : substring -> Word8Vector.vector

    val decodeStrict : string -> Word8Vector.vector
    val decodeSliceStrict : substring -> Word8Vector.vector

## Description

`val isBase64 : char -> bool`

  `isBase64 c` returns true if the character `c` is in the Base-64 alphabet (i.e., a letter, digit, or a slash or plus character). Note that the padding character (the equals sign) is not considered to be in the alphabet.

`val encode : Word8Vector.vector -> string`

  `encode bv` returns a string that is the Base-64 encoding of the byte vector `bv`.

`val encodeSlice : Word8VectorSlice.slice -> string`

  `encode bvs` returns a string that is the Base-64 encoding of the byte-vector slice `bvs`.

`exception Incomplete`

  This exception is raised by the decoding functions if a Base-64 string does not end in a complete encoding quantum (i.e., four characters including padding characters).

`exception Invalid of (int * char)`

  This exception is raised by the decoding functions if an invalid Base-64 character is encountered. The int is the position of the character and the char is the invalid character.

`val decode : string -> Word8Vector.vector`

  `decode s` returns the result of decoding the Base-64 string `s`. This function ignores whitespace (e.g., line breaks), but will raise the `Incomplete` exception if the last quantum is incomplete.

`val decodeSlice : substring -> Word8Vector.vector`

  `decode ss` returns the result of decoding the Base-64 substring `ss`. This function ignores whitespace (e.g., line breaks), but will raise the `Incomplete` exception if the last quantum is incomplete.

`val decodeStrict : string -> Word8Vector.vector`

  `decodeStrict s` returns the result of decoding the Base-64 string `s`. The string `s` maust only contain valid Base-64 characters, otherwise the `Invalid` exception is raised. This function will also raise the `Incomplete` exception if the last quantum is incomplete.

`val decodeSliceStrict : substring -> Word8Vector.vector`

  `decode ss` returns the result of decoding the Base-64 substring `ss`. The string `s` maust only contain valid Base-64 characters, otherwise the `Invalid` exception is raised. This function will also raise the `Incomplete` exception if the last quantum is incomplete.

## See Also

The Util Library

SML/NJ Version 110.99.9 (November 4, 2025)
Last updated 2025-11-04 15:48:25 UTC