Base64 Encoding in GDScript

Our first example demonstrates base64 encoding and decoding. GDScript doesn’t have a built-in base64 module, so we’ll use a custom implementation.

extends Node

# This is a simple base64 implementation for GDScript
# It's not as efficient as built-in methods in other languages,
# but it serves the purpose for this example
const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

func _ready():
    # Here's the string we'll encode/decode
    var data = "abc123!?$*&()'-=@~"
    
    # Encode using the standard base64 encoder
    var s_enc = base64_encode(data)
    print(s_enc)
    
    # Decoding
    var s_dec = base64_decode(s_enc)
    print(s_dec)
    print()
    
    # This encodes/decodes using a URL-compatible base64 format
    var u_enc = base64_url_encode(data)
    print(u_enc)
    var u_dec = base64_url_decode(u_enc)
    print(u_dec)

func base64_encode(input: String) -> String:
    var output = ""
    var i = 0
    while i < input.length():
        var n = (ord(input[i]) << 16) if i + 0 < input.length() else 0
        n += (ord(input[i + 1]) << 8) if i + 1 < input.length() else 0
        n += ord(input[i + 2]) if i + 2 < input.length() else 0
        
        output += BASE64_CHARS[(n >> 18) & 63]
        output += BASE64_CHARS[(n >> 12) & 63]
        output += BASE64_CHARS[(n >> 6) & 63] if i + 1 < input.length() else "="
        output += BASE64_CHARS[n & 63] if i + 2 < input.length() else "="
        
        i += 3
    
    return output

func base64_decode(input: String) -> String:
    var output = ""
    var i = 0
    while i < input.length():
        var n = (BASE64_CHARS.find(input[i]) << 18) if i + 0 < input.length() else 0
        n += (BASE64_CHARS.find(input[i + 1]) << 12) if i + 1 < input.length() else 0
        n += (BASE64_CHARS.find(input[i + 2]) << 6) if i + 2 < input.length() else 0
        n += BASE64_CHARS.find(input[i + 3]) if i + 3 < input.length() else 0
        
        output += char((n >> 16) & 255)
        if input[i + 2] != "=":
            output += char((n >> 8) & 255)
        if input[i + 3] != "=":
            output += char(n & 255)
        
        i += 4
    
    return output

func base64_url_encode(input: String) -> String:
    return base64_encode(input).replace("+", "-").replace("/", "_").replace("=", "")

func base64_url_decode(input: String) -> String:
    var padded = input.replace("-", "+").replace("_", "/")
    while padded.length() % 4 != 0:
        padded += "="
    return base64_decode(padded)

This script defines functions for both standard and URL-compatible base64 encoding and decoding. Here’s what each part does:

  1. We define a constant BASE64_CHARS containing the characters used in base64 encoding.

  2. In the _ready() function, we demonstrate the usage of our base64 functions:

    • We encode and then decode a string using standard base64.
    • We do the same using URL-compatible base64.
  3. The base64_encode() function takes a string input and returns its base64 encoded form.

  4. The base64_decode() function takes a base64 encoded string and returns the original string.

  5. The base64_url_encode() function is similar to base64_encode(), but replaces characters to make the output URL-safe.

  6. The base64_url_decode() function reverses the URL-safe encoding.

To run this script, you would need to attach it to a Node in your Godot scene. The output would be printed to the Godot console.

Note that this implementation is not as efficient as built-in methods in other languages, but it demonstrates the concept of base64 encoding and decoding in GDScript.