Module Webs_base64

base64 and base64url codecs.

As defined in RFC 4684, with or without padding. Decoding errors on non-canonical encodings to ensure that no two different encodings decode to the same byte sequence (see this paper).

Encoding and padding specification

type encoding = [
  1. | `Base64
    (*

    base64

    *)
  2. | `Base64url
]

The type for encodings.

type padding = [
  1. | `Padded
    (*

    Padded with '=' characters.

    *)
  2. | `Unpadded
    (*

    Not padded.

    *)
]

The type for specifing padding.

Decoding errors

type error =
  1. | Invalid_length of int
    (*

    Invalid input length

    *)
  2. | Invalid_letter of char * int
    (*

    Invalid letter at index.

    *)
  3. | Non_canonical_encoding

The type for decoding errors.

val error_message : encoding -> error -> string

error_message enc e is an error message for error e while decoding encoding enc.

base64

val encode : padding -> string -> string

encode p s is the base64 encoding of s padded according to p.

val decode' : padding -> string -> (string, error) Stdlib.result

decode' p s is the base64 decode of s. If p is:

  • `Padding the decode expects a padded encoding. The decode errors with Invalid_length if the padding is missing.
  • `Unpadded the decode expect no padding. The decode errors with Invalid_letter if there is a padding.

All decodes error with Non_canonical_encoding if a padding letter has a non-zero padding.

val decode : padding -> string -> (string, string) Stdlib.result

decode is like decode' with errors mapped by error_message `Base64.

base64url

val encode_base64url : padding -> string -> string

encode_base64url is like encode but for the base64url encoding.

val decode_base64url' : padding -> string -> (string, error) Stdlib.result

decode_base64url' is like decode but for the base64url encoding.

val decode_base64url : padding -> string -> (string, string) Stdlib.result

decode_base64url is like decode_base64url' with errors mapped by error_message `Base64url.