|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "base64" for Base64
|
|
|
|
System.print("=== Base64 Module Demo ===\n")
|
|
|
|
System.print("--- Basic Encoding ---")
|
|
var text = "Hello, World!"
|
|
var encoded = Base64.encode(text)
|
|
System.print("Original: %(text)")
|
|
System.print("Encoded: %(encoded)")
|
|
|
|
System.print("\n--- Basic Decoding ---")
|
|
var decoded = Base64.decode(encoded)
|
|
System.print("Decoded: %(decoded)")
|
|
|
|
System.print("\n--- Various Strings ---")
|
|
var samples = [
|
|
"Hello",
|
|
"The quick brown fox jumps over the lazy dog",
|
|
"12345",
|
|
"Special chars: !@#^&*()",
|
|
"Unicode: cafe resume naive"
|
|
]
|
|
|
|
for (sample in samples) {
|
|
var enc = Base64.encode(sample)
|
|
var dec = Base64.decode(enc)
|
|
System.print("%(sample)")
|
|
System.print(" -> %(enc)")
|
|
System.print(" <- %(dec)")
|
|
System.print("")
|
|
}
|
|
|
|
System.print("--- URL-Safe Encoding ---")
|
|
var urlData = "Hello+World/Test=End"
|
|
var urlEncoded = Base64.encodeUrl(urlData)
|
|
var standardEncoded = Base64.encode(urlData)
|
|
System.print("Original: %(urlData)")
|
|
System.print("Standard Base64: %(standardEncoded)")
|
|
System.print("URL-Safe Base64: %(urlEncoded)")
|
|
|
|
System.print("\n--- URL-Safe Decoding ---")
|
|
var urlDecoded = Base64.decodeUrl(urlEncoded)
|
|
System.print("Decoded from URL-safe: %(urlDecoded)")
|
|
|
|
System.print("\n--- Binary-like Data ---")
|
|
var binaryText = "Binary: \x00\x01\x02\xFF"
|
|
var binaryEncoded = Base64.encode(binaryText)
|
|
System.print("Binary-like string encoded: %(binaryEncoded)")
|
|
|
|
System.print("\n--- Empty String ---")
|
|
System.print("Empty encoded: %(Base64.encode(""))")
|
|
System.print("Empty decoded: %(Base64.decode(""))")
|
|
|
|
System.print("\n--- Practical Example: Basic Auth Header ---")
|
|
var username = "admin"
|
|
var password = "secret123"
|
|
var credentials = "%(username):%(password)"
|
|
var authHeader = "Basic " + Base64.encode(credentials)
|
|
System.print("Credentials: %(credentials)")
|
|
System.print("Authorization header: %(authHeader)")
|
|
|
|
System.print("\n--- Practical Example: Data URI ---")
|
|
var jsonData = "{\"message\":\"Hello\"}"
|
|
var dataUri = "data:application/json;base64," + Base64.encode(jsonData)
|
|
System.print("JSON: %(jsonData)")
|
|
System.print("Data URI: %(dataUri)")
|