This is a study-focused WebAssembly library designed to explore the bridge between WebAssembly (Wasm) and JavaScript. It provides a simple interface to encrypt and decrypt data using AES-128/CBC/PKCS7 and Base64 encoding. The library is written in Rust and compiled to WebAssembly, with a JavaScript bridge to interact with the Wasm module.
https://guinetik.github.io/crypto.wasm/
- AES-128 Encryption: Encrypts data using AES-128 in CBC mode with PKCS7 padding.
- Base64 Encoding/Decoding: Supports Base64 encoding and decoding as an alternative to encryption.
- JavaScript Bridge: Includes a custom JavaScript interface (
crypto_wasm.js
) to interact with the Wasm module. - CLI Support: Includes a command-line interface (CLI) for encrypting, decrypting, encoding, and decoding data.
- Study Focus: This started as a study on traits. Then I wanted to check interoperability of the CLI and WASM. In short, this project demonstrates how to bridge WebAssembly and JavaScript for cryptographic operations.
-
Rust: Install Rust using rustup.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
WebAssembly Target: Add the WebAssembly target for Rust.
rustup target add wasm32-unknown-unknown
-
wasm-pack: Install
wasm-pack
for building and packaging the WebAssembly module.cargo install wasm-pack
-
Terser: Install
terser
for minifying JavaScript files.npm install -g terser
To run the tests, use the following command:
cargo test
The build.ps1
script automates the build process for Windows users. It runs tests, builds the WebAssembly module, minifies the JavaScript files, and prepares the final distribution package.
- Open PowerShell.
- Navigate to the project directory.
- Run the script:
.\build.ps1
The final package will be located in the dist/crypto_wasm
directory.
-
Run tests:
cargo test
-
Build the WebAssembly module:
wasm-pack build --target web --out-dir build/crypto_wasm
-
Minify the generated JavaScript files:
terser build/crypto_wasm/crypto_wasm.js -o build/crypto_wasm/crypto_wasm.lib.min.js --compress --mangle terser js/crypto_wasm.js -o build/crypto_wasm/crypto_wasm.min.js --compress --mangle
-
Prepare the distribution package:
mkdir -p dist/crypto_wasm cp build/crypto_wasm/crypto_wasm.lib.min.js dist/crypto_wasm/ cp build/crypto_wasm/crypto_wasm_bg.wasm dist/crypto_wasm/ cp build/crypto_wasm/crypto_wasm.min.js dist/crypto_wasm/
-
Include the generated wrapper file in your html file:
<script src="path/to/crypto_wasm.min.js"></script>
-
Initialize the Wasm module and use the
CryptoWasm
class:const wasmPath = window.location.href + "/dist/crypto_wasm/crypto_wasm.lib.min.js"; // Initialize CryptoWasm const cryptoWasm = new CryptoWasmWrapper(); await cryptoWasm.init(wasmPath); // Create a new CryptoWasm instance const crypto = new CryptoWasm("thisisasecretkey", EncryptorType.Aes128); // Encrypt a token const encrypted = crypto.cypher("my secret token"); console.log("Encrypted:", encrypted); // Decrypt a token const decrypted = crypto.decypher(encrypted); console.log("Decrypted:", decrypted);
Before using the CLI, you must build the project using the release profile:
cargo build --release
A compiled binary will be available in the target/release
directory (crypto_cli.exe on Windows).
The CLI supports both AES-128 encryption/decryption and Base64 encoding/decoding.
crypto_cli --encryptor aes128 --input "Hello, World!" --key "thisisasecretkey"
Output:
Encrypted: a238c6ee47d012c384fdd534ae0ddb6f:5c6feee9034441cb883170a0837ce8f2
crypto_cli --encryptor aes128 --input "ENCRYPTED VALUE" --key "thisisasecretkey" -d
Output:
Decrypted: Hello, World!
./target/release/crypto_cli --encryptor base64 --input "Hello, World!"
Output:
Encrypted: SGVsbG8sIFdvcmxkIQ==
crypto_cli --encryptor base64 --input "SGVsbG8sIFdvcmxkIQ==" -d
Output:
Decrypted: Hello, World!
src/lib.rs
: Contains the Rust implementation of AES-128 and Base64 operations.src/main.rs
: Implements the CLI interface.js/crypto_wasm.js
: JavaScript bridge for interacting with the Wasm module.build.ps1
: PowerShell script for automating the build process on Windows.dist/crypto_wasm/
: Contains the final distribution files:crypto_wasm.lib.min.js
: Minified WebAssembly loader.crypto_wasm.min.js
: Minified JavaScript bridge.crypto_wasm_bg.wasm
: Compiled WebAssembly module.
This project is a study tool, but contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Rust for providing a safe and performant programming language.
- wasm-pack for simplifying WebAssembly packaging.
- aes and block-modes crates for encryption functionality.