Symmetric-key encryption converts readable plaintext into ciphertext and reverses it with the same secret key. In a database, it can protect stored data while allowing authorized retrieval. section 5.3.1
A Wasm UDF implementation follows this sequence:
- Create an encryption instance from the supplied key. section 5.3.1
- Encrypt plaintext and return the result as a Base64 string. section 5.3.1
- Decode ciphertext with the same key and return plaintext, or report access denial when decryption fails. section 5.3.1
Listing 5.4 lib.rs file of our wasm_udf_example project
use libsql_bindgen::*;
use magic_crypt::{new_magic_crypt, MagicCryptTrait};
#[libsql_bindgen::libsql_bindgen]
pub fn encrypt(data: String, key: String) -> String {
let mc = new_magic_crypt!(key, 256);
mc.encrypt_str_to_base64(data)
}
#[libsql_bindgen::libsql_bindgen]
pub fn decrypt(data: String, key: String) -> String {
let mc = new_magic_crypt!(key, 256);
mc.decrypt_base64_to_string(data)
.unwrap_or("[ACCESS DENIED]".to_owned())
}
The complete Rust library exposes encrypt and decrypt as libSQL user-defined functions, using Magic Crypt with 256-bit strength. section 5.3.1





