Base64 Encode/Decode

Encoding

Encode and decode Base64 strings, with URL-safe and buffer support.

Import

Import
import { base64 } from 'toolmetry';

API Reference

FunctionParametersReturnsDescription
base64.encodeinput: string, encoding?: stringstringEncode a string to Base64
base64.decodeinput: string, encoding?: stringstringDecode a Base64 string
base64.encodeURLinput: stringstringEncode to URL-safe Base64
base64.decodeURLinput: stringstringDecode URL-safe Base64
base64.isValidinput: stringbooleanCheck if a string is valid Base64
base64.encodeBufferbuffer: ArrayBuffer | Uint8ArraystringEncode buffer to Base64
base64.decodeToBufferinput: stringUint8ArrayDecode Base64 to Uint8Array

Examples

Basic Encode/Decode
import { base64 } from 'toolmetry';

const encoded = base64.encode('Hello, World!');
// "SGVsbG8sIFdvcmxkIQ=="

const decoded = base64.decode(encoded);
// "Hello, World!"
URL-Safe Base64
import { base64 } from 'toolmetry';

const urlSafe = base64.encodeURL('Hello+World/Test=');
// "SGVsbG8rV29ybGQvVGVzdA"

const original = base64.decodeURL(urlSafe);
// "Hello+World/Test="
Validation
import { base64 } from 'toolmetry';

base64.isValid('SGVsbG8='); // true
base64.isValid('not-base64!'); // false
Buffer Operations
import { base64 } from 'toolmetry';

const buffer = new Uint8Array([72, 101, 108, 108, 111]);
const encoded = base64.encodeBuffer(buffer);
// "SGVsbG8="

const decoded = base64.decodeToBuffer(encoded);
// Uint8Array [72, 101, 108, 108, 111]

Try It Live