Base64

Bytes

Base64 encode and decode for Bytes.

encode / decode use the standard alphabet with padding. encodeUrl / decodeUrl use the URL-safe alphabet (- / _) and omit padding — the same shape JWT and many webhook APIs expect.

Values

encode

encode : Bytes -> String

Encode bytes as a standard Base64 string (with = padding).

Examples

encode (Bytes.Encode.encode (Bytes.Encode.string "hi")) == "aGk="

decode

decode : String -> Maybe Bytes

Decode a standard Base64 string. Returns Nothing on invalid input.

Examples

decode "aGk="
    |> Maybe.map (\b -> encode b) == Just "aGk="

decode "!!!" == Nothing

encodeUrl

encodeUrl : Bytes -> String

Encode bytes as URL-safe Base64 without padding.

Examples

encodeUrl (Bytes.Encode.encode (Bytes.Encode.string "hi")) == "aGk"

decodeUrl

decodeUrl : String -> Maybe Bytes

Decode a URL-safe Base64 string (padding optional). Returns Nothing on invalid input.

Examples

decodeUrl "aGk"
    |> Maybe.map (\b -> encode b) == Just "aGk="

decodeUrl "!!!" == Nothing