C3 AI Documentation Home

Base64

Base64 encoding is a common method for converting binary data into a text format, which is essential for transmitting data over media that are primarily designed to handle text, such as HTTP or email.

The Base64 Type provides methods for encoding and decoding in Base64. This Type covers Base64 operations such as encoding and decoding strings and bytes, specific functions for URL-safe encoding and decoding, and even handling integers.

The use cases of the Base64 Type showcase its utility in scenarios involving web development, URL handling, and data storage optimization. By providing methods for both standard and URL-safe encoding, including specific functionalities for handling strings and integers, the Base64 Type proves to be an indispensable tool in modern software development, enhancing both functionality and security in data handling processes.

Standard alphabet

This section highlights the capabilities of the Base64 Type in encoding and decoding binary data using the standard Base64 alphabet. This alphabet includes a specific set of characters used in the encoding process.

Encoding zeroes to Base64

This section demonstrates encoding 25 bytes of zeroes into a Base64 string, resulting in a string of As followed by == padding. This is because Base64 represents zero bytes as A.

This line encodes 25 bytes of zeroes into a Base64 string. The result is a string of As followed by == padding, as Base64 represents zero bytes as A.

JavaScript
Base64.encode(Bin.zeroes(25)) === 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='

Encoding hexadecimal to Base64

Encoding hexadecimal to Base64 showcases the conversion of hexadecimal data into binary, followed by encoding to a Base64 string, reflecting how byte values are translated into the Base64 alphabet.

In the code snippet below, hexadecimal data is converted into binary and then encoded to a Base64 string. The hex data represents specific byte values, which are translated into the Base64 alphabet.

JavaScript
Base64.encode(Bin.fromHex('aaffbbeeccdd00')) === 'qv+77szdAA=='

Short Data Encoding

Illustrates encoding of shorter data (for example, two bytes of ff), resulting in a short Base64 string with padding.

This snippet shows how shorter data (in this case, two bytes of ff) is encoded. The result is a short Base64 string with padding (=).

JavaScript
Base64.encode(Bin.fromHex('ffff')) === '//8='

Decoding Invalid Base64 String

An example where decoding an invalid Base64 string ("50%") throws an exception, highlighting the strict adherence to Base64 encoding rules.

An attempt to decode an invalid Base64 string (here, "50%") results in an exception, as the string does not conform to Base64 encoding rules.

JavaScript
Base64.decode("50%", true) // throws InvalidValue

Decoding Base64 to zeroes

This section demonstrates the reversibility of the process by decoding a Base64 string of As back to 25 bytes of zeroes.

This decodes a Base64 string of As back to 25 bytes of zeroes, demonstrating the reversibility of the process.

JavaScript
Base64.decode('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==', true) === Bin.zeroes(25)

Decoding Base64 strings

This section highlights various examples of decoding Base64 strings back to their original binary format, showcasing the accuracy and reliability of the Base64 Type in encoding/decoding processes.

These examples decode various Base64 strings back to their original binary (in hex format), illustrating the accuracy and reliability of the encoding/decoding process.

JavaScript
Base64.decode('qv+77szd') === Bin.fromHex('aaffbbeeccdd')
Base64.decode('qv+77szdAA==') === Bin.fromHex('aaffbbeeccdd00')
Base64.decode('//8=') === Bin.fromHex('ffff')

URL-safe alphabet

This topic focuses on encoding and decoding techniques that are safe for use in URLs. This variant of Base64 encoding modifies the standard alphabet to make it suitable for URL usage. Key aspects include:

URL-safe Encoding of zeroes

Similar to the standard method but excludes padding characters, making the encoded string safe for URLs. Similar to standard encoding, but without padding characters, making it safe for URLs.

JavaScript
Base64.encodeUrlSafe(Bin.zeroes(25)) === 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

URL-safe encoding of hex data

This section covers URL-safe encoding of hexadecimal data into a URL-safe Base64 string, using - and _ instead of + and / to ensure URL compatibility. Encodes hexadecimal data to a URL-safe Base64 string. Note the use of - and _ instead of + and /.

JavaScript
Base64.encodeUrlSafe(Bin.fromHex('aaffbbeeccdd00')) === 'qv-77szdAA';
Base64.encodeUrlSafe(Bin.fromHex('ffff')) === '__8';

Decoding URL-safe Base64 strings

Examples of decoding URL-safe Base64 strings back to binary data, along with error handling in cases of invalid strings.

These examples show decoding of URL-safe Base64 strings back to binary data. They also demonstrate the error handling in case of invalid strings.

JavaScript
Base64.decodeUrlSafe("50%", true) // throws InvalidValue
Base64.decodeUrlSafe('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', true) === Bin.zeroes(25);
Base64.decodeUrlSafe('qv-77szd') === Bin.fromHex('aaffbbeeccdd');
Base64.decodeUrlSafe('qv-77szdAA') === Bin.fromHex('aaffbbeeccdd00');
Base64.decodeUrlSafe('__8') === Bin.fromHex('ffff');

The Base64 Type is a powerful and versatile tool for encoding and decoding binary data in various formats, making it an essential utility in modern programming for data transmission and storage. Its ability to handle standard and URL-safe alphabets broadens its application scope, ensuring compatibility and safety in diverse computing environments.

Use cases and implementations of Base64 Type

The Base64 Type, as previously outlined, provides a comprehensive set of functionalities for encoding and decoding binary data to and from strings. This topic extends the basic use cases to more advanced real-world scenarios, demonstrating the versatility and applicability of the Base64 Type in practical situations.

Encoding and decoding user credentials

In web development, it's common to encode user credentials before transmitting them over HTTP for basic authentication.

JavaScript
// Username and password are combined into a single string "username:password"
let credentials = "user@example.com:password123";

// The credentials string is then encoded into Base64
let encodedCredentials = Base64.encodeString(credentials, "UTF-8");

// The encoded credentials can be used in HTTP headers
let authHeader = "Authorization: Basic " + encodedCredentials;

// Decoding the credentials (server-side)
let decodedCredentials = Base64.decodeString(encodedCredentials, "UTF-8", true, "credentials");
  • encodeString: Encodes the combined username and password into a Base64 string.
  • decodeString: Decodes the Base64 string back to the original credentials. Useful on the server side to authenticate the user.

Safe URL Parameter Encoding

Encoding binary data, like a small image or a file, into a URL-safe format for use in GET requests or in web applications.

JavaScript
// Assuming `binaryData` is a binary representation of a small file
let binaryData = Bin.fromHex('...'); // Binary data in hexadecimal format

// Encoding the data into a URL-safe Base64 string
let encodedUrlData = Base64.encodeUrlSafe(binaryData);

// Appending the encoded data to a URL
let url = "https://example.com/data?file=" + encodedUrlData;

// Decoding the data (server-side)
let decodedData = Base64.decodeUrlSafe(encodedUrlData, true);
  • encodeUrlSafe: Converts binary data into a URL-safe Base64 encoded string.
  • decodeUrlSafe: Decodes the URL-safe Base64 string back to binary data, useful for server-side processing.

Encoding and decoding integer values for compact storage

Storing large integer values in a compact form in databases or in configuration files.

JavaScript
// An example integer value
let intValue = 123456789;

// Encoding the integer to a Base64 string
let encodedInt = Base64.encodeInt(intValue);

// Storing the encoded integer
let storageValue = encodedInt; // Can be stored in a database or a file

// Decoding the integer (when retrieving)
let decodedInt = Base64.decodeInt(storageValue, true, "integer");
  • encodeInt: Encodes an integer value into a Base64 string, reducing the space required for storage.
  • decodeInt: Decodes the Base64 string back into an integer.
Was this page helpful?