Skip to content
Lattice

Base64

Git Source

Authors: Solady (https://github.com/vectorized/solady/blob/main/src/utils/Base64.sol), Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/Base64.sol), Modified from (https://github.com/Brechtpd/base64/blob/main/base64.sol) by Brecht Devos - <brecht@loopring.org>.

Library to encode strings in Base64.

Functions

encode

Encodes data using the base64 encoding described in RFC 4648. See: https://datatracker.ietf.org/doc/html/rfc4648

function encode(bytes memory data, bool fileSafe, bool noPadding)
        internal
        pure
        returns (string memory result);

Parameters

NameTypeDescription
databytes
fileSafeboolWhether to replace '+' with '-' and '/' with '_'.
noPaddingboolWhether to strip away the padding.

Returns

NameTypeDescription
resultstring

encode

Encodes data using the base64 encoding described in RFC 4648. Equivalent to encode(data, false, false).

function encode(bytes memory data) internal pure returns (string memory result);

Parameters

NameTypeDescription
databytes

Returns

NameTypeDescription
resultstring

encode

Encodes data using the base64 encoding described in RFC 4648. Equivalent to encode(data, fileSafe, false).

function encode(bytes memory data, bool fileSafe) internal pure returns (string memory result);

Parameters

NameTypeDescription
databytes
fileSafebool

Returns

NameTypeDescription
resultstring

decode

Decodes base64 encoded data.

Supports:

  • RFC 4648 (both standard and file-safe mode).
  • RFC 3501 (63: ',').

Does not support:

  • Line breaks.

Note: For performance reasons, this function will NOT revert on invalid data inputs. Outputs for invalid inputs will simply be undefined behaviour. It is the user's responsibility to ensure that the data is a valid base64 encoded string.

function decode(string memory data) internal pure returns (bytes memory result);

Parameters

NameTypeDescription
datastring

Returns

NameTypeDescription
resultbytes