/*
  Example 33: Base64 Encoding, Decoding, and Encryption

  Demonstrates:
  1. Text Base64 encoding/decoding with std/base64
  2. Byte-array Base64 encoding/decoding with std/base64
  3. Reading a file as Base64 with read_file(path, "base64")
  4. Encrypting and decrypting private string content
*/

allow "std/base64" in as Base64
allow "std/time" in with {sleep}

show "=== 1. Text to Base64 ==="
let text = "Hello from Sesi"
let encoded_text = text | Base64.encode
let decoded_text = encoded_text | Base64.decode
show "Original:" text
show "Encoded: " encoded_text
show "Decoded: " decoded_text
show ""
sleep(1000)

show "=== 2. Bytes to Base64 ==="
let bytes = [0, 255, 16, 32, 127]
let encoded_bytes = bytes | Base64.encode("bytes")
let decoded_bytes = encoded_bytes | Base64.decode("bytes")
show "Input bytes:  " str(bytes)
show "Base64 bytes: " encoded_bytes
show "Output bytes: " str(decoded_bytes)
show ""
sleep(1000)

show "=== 3. Image to Base64 ==="
let file_b64 = "docs/notebook.png" | read_file("base64")
let file_preview = file_b64 | slice(0, 80)
show "File base64 preview:" file_preview "..."
show "File length: " str(len(file_b64)) " characters"
show ""

show "=== 4. Encrypt and Decrypt Private Text ==="
let private_text = "launch-code: SESI-42"
let password = "example-passphrase"
let encrypted_text = private_text | encrypt(password)
let decrypted_text = encrypted_text | decrypt(password)
show "Original private text:" private_text
show "Encrypted envelope:" encrypted_text
show "Decrypted private text:" decrypted_text
show ""

show "Done."
