Base64 Functions and Properties

The Base64 object provides functions for encoding and decoding data using Base64 encoding. Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. These functions are essential when working with APIs that require Base64-encoded data, handling binary data in JSON, or transmitting binary data as text.

String Encoding and Decoding

Base64.encode()

Encodes a string using Base64 encoding.

This function is similar to the JavaScript base64 encode.

Syntax:

Base64.encode(string)

Parameters:

  • string - The string to encode

Returns: A Base64-encoded string

Example:

Base64.encode($City)
// Where $City = "Pleasanton"
// Returns: "UGxlYXNhbnRvbg=="

Base64.encode("Hello, World!")
// Returns: "SGVsbG8sIFdvcmxkIQ=="

Base64.encode($username + ":" + $password)
// Returns: Base64-encoded credentials for Basic Auth

Base64.decode()

Decodes a Base64-encoded string back to its original string value.

This function is similar to the JavaScript Base64 decode.

Syntax:

Base64.decode(encodedString)

Parameters:

  • encodedString - The Base64-encoded string to decode

Returns: The decoded string

Example:

Base64.decode($City)
// Where $City = "UGxlYXNhbnRvbg=="
// Returns: "Pleasanton"

Base64.decode("SGVsbG8sIFdvcmxkIQ==")
// Returns: "Hello, World!"

Binary Data Encoding and Decoding

Base64.encodeAsBinary()

Encodes binary data and returns a Base64 string.

Syntax:

Base64.encodeAsBinary(binaryData)

Parameters:

  • binaryData - The binary data (byte array) to encode

Returns: A Base64-encoded string representation of the binary data

Example:

Base64.encodeAsBinary($binaryData)
// Where $binaryData contains byte array
// Returns: {"_snaptype_binary_base64": "MjM0"}

Base64.decodeAsBinary()

Decodes a Base64-encoded string and returns it as a byte array.

Syntax:

Base64.decodeAsBinary(encodedString)

Parameters:

  • encodedString - The Base64-encoded string to decode

Returns: A byte array representing the decoded binary data

Example:

Base64.decodeAsBinary($encodedData)
// Returns: {"_snaptype_binary_base64": "dgrfbvfr"}

GZip Compression with Base64

Base64.decodeGZip()

Unzips and decodes a GZip-compressed Base64 string.

Syntax:

Base64.decodeGZip(gzipEncodedString)

Parameters:

  • gzipEncodedString - A GZip-compressed Base64-encoded string

Returns: The decompressed and decoded string

Example:

Base64.decodeGZip($compressedData)
// Where $compressedData is a GZipped Base64 string
// Returns: "Pleasanton"

Base64.decodeGZipAsBinary()

Unzips and decodes a GZip-compressed Base64 string, returning it as a byte array.

Syntax:

Base64.decodeGZipAsBinary(gzipEncodedString)

Parameters:

  • gzipEncodedString - A GZip-compressed Base64-encoded string

Returns: The decompressed data as a byte array

Example:

Base64.decodeGZipAsBinary($compressedBinary)
// Where $compressedBinary is a GZipped Base64 byte array
// Returns: "Pleasanton"

Common Use Cases

HTTP Basic Authentication:

// Generate Authorization header
"Basic " + Base64.encode($username + ":" + $password)

API Token Encoding:

// Encode API credentials
{
  "Authorization": "Basic " + Base64.encode($apiKey + ":"),
  "Content-Type": "application/json"
}

Decode Incoming Base64 Data:

// Decode Base64-encoded field from API response
Base64.decode($response.encodedData)

Handle Binary Data in JSON:

// Encode binary file content for JSON transmission
{
  "filename": $fileName,
  "content": Base64.encodeAsBinary($fileContent)
}

Decode Compressed Data:

// Decompress and decode GZipped Base64 data
Base64.decodeGZip($compressedPayload)

Image Data Embedding:

// Create data URI for embedded image
"data:image/png;base64," + Base64.encode($imageData)

Function Comparison

Function Input Type Output Type Use Case
encode() String Base64 string Encode text data
decode() Base64 string String Decode text data
encodeAsBinary() Binary data Base64 string Encode binary/files
decodeAsBinary() Base64 string Binary data Decode to binary
decodeGZip() GZipped Base64 String Decompress text
decodeGZipAsBinary() GZipped Base64 Binary data Decompress binary

Best Practices

  • String vs Binary: Use encode() and decode() for text data. Use encodeAsBinary() and decodeAsBinary() for binary data like images or files.
  • Authentication: Base64 encoding is NOT encryption. Do not use Base64 alone to protect sensitive data. Always use HTTPS for transmitting Base64-encoded credentials.
  • Size Overhead: Base64 encoding increases data size by approximately 33%. Consider this when working with large files or bandwidth-constrained environments.
  • Compression: Use decodeGZip() functions when the API or source provides GZip-compressed Base64 data to reduce transfer size.
  • Error Handling: Invalid Base64 strings will cause decoding errors. Validate input before decoding or use error handling.
  • Performance: Base64 encoding/decoding is computationally intensive for large data. Minimize encoding/decoding operations in high-volume pipelines.
  • Character Sets: Base64 uses a specific character set (A-Z, a-z, 0-9, +, /, =). Ensure data hasn't been corrupted during transmission.

Encoding Flow Patterns

Round-Trip Encoding:

// Encode then decode should return original value
Base64.decode(Base64.encode("Hello"))  // Returns: "Hello"

Multi-Step Authentication:

// Build Basic Auth header
{
  "step1_combine": $username + ":" + $password,
  "step2_encode": Base64.encode($username + ":" + $password),
  "step3_header": "Basic " + Base64.encode($username + ":" + $password)
}

Decode and Process:

// Decode Base64 then parse as JSON
JSON.parse(Base64.decode($encodedJsonPayload))

Conditional Encoding:

// Encode only if not already encoded
$data.includes("==") ? $data : Base64.encode($data)

Security Considerations

  • Not Encryption: Base64 is an encoding scheme, not encryption. Encoded data can be easily decoded by anyone.
  • Credentials: Never store Base64-encoded credentials as a security measure. Use proper encryption and secure storage.
  • HTTPS Required: Always use HTTPS when transmitting Base64-encoded sensitive data to prevent interception.
  • Validation: Validate decoded data to ensure it hasn't been tampered with during transmission.
  • Token Management: Treat Base64-encoded tokens with the same security as plaintext tokens.

Working with Different Data Types

Text Data:

// Simple text encoding
Base64.encode("Configuration: enabled=true")

// Decode configuration string
Base64.decode($encodedConfig)

Binary Data:

// Encode file or image content
Base64.encodeAsBinary($fileContent)

// Decode to binary for file writing
Base64.decodeAsBinary($encodedFile)

Compressed Data:

// Handle compressed payloads from APIs
Base64.decodeGZip($response.compressedData)

// Decompress binary data
Base64.decodeGZipAsBinary($compressedBinary)

JSON Data:

// Encode JSON as Base64 string
Base64.encode(JSON.stringify($object))

// Decode Base64 then parse JSON
JSON.parse(Base64.decode($encodedJson))