Base64 Encoding
Base64 Encoding
Section titled “Base64 Encoding”Encode binary data to base64 strings and decode base64 back to binary. Uses standard base64 encoding per RFC 4648.
Loading
Section titled “Loading”local base64 = require("base64")Encoding
Section titled “Encoding”Encode Data
Section titled “Encode Data”Encodes a string (including binary data) to base64.
-- Encode textlocal encoded = base64.encode("Hello, World!")print(encoded) -- "SGVsbG8sIFdvcmxkIQ=="
-- Encode binary data (e.g., from file)local image_data = fs.read_binary("photo.jpg")local image_b64 = base64.encode(image_data)
-- Encode JSON for transportlocal json = require("json")local payload = json.encode({user = "alice", action = "login"})local token_part = base64.encode(payload)
-- Encode credentialslocal credentials = base64.encode("username:password")local auth_header = "Basic " .. credentials| Parameter | Type | Description |
|---|---|---|
data | string | Data to encode (text or binary) |
Returns: string, error - Empty string input returns empty string.
Decoding
Section titled “Decoding”Decode Data
Section titled “Decode Data”Decodes a base64 string back to original data.
-- Decode textlocal decoded = base64.decode("SGVsbG8sIFdvcmxkIQ==")print(decoded) -- "Hello, World!"
-- Decode with error handlinglocal data, err = base64.decode(user_input)if err then return nil, errors.new("INVALID", "Invalid base64 data")end
-- Decode binary datalocal image_b64 = request.bodylocal image_data, err = base64.decode(image_b64)if err then return nil, errendfs.write_binary("output.jpg", image_data)
-- Decode JWT partslocal parts = string.split(jwt_token, ".")local header = json.decode(base64.decode(parts[1]))local payload = json.decode(base64.decode(parts[2]))| Parameter | Type | Description |
|---|---|---|
data | string | Base64 encoded string |
Returns: string, error - Empty string input returns empty string.
Errors
Section titled “Errors”| Condition | Kind | Retryable |
|---|---|---|
| Input not a string | errors.INVALID | no |
| Invalid base64 characters | errors.INVALID | no |
| Corrupted padding | errors.INVALID | no |
See Error Handling for working with errors.