# Rust WebAssembly Cryptography Module This project provides a WebAssembly module written in Rust that offers cryptographic functionality for web applications. ## Features - **Asymmetric Cryptography** - ECDSA keypair generation - Message signing - Signature verification - **Symmetric Cryptography** - ChaCha20Poly1305 encryption/decryption - Secure key generation ## Prerequisites Before you begin, ensure you have the following installed: - [Rust](https://www.rust-lang.org/tools/install) (1.70.0 or later) - [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) (0.10.0 or later) - [Node.js](https://nodejs.org/) (14.0.0 or later) - A modern web browser that supports WebAssembly ## Project Structure ``` webassembly/ ├── src/ │ ├── api/ # Public API modules │ │ ├── keypair.rs # Public keypair API │ │ ├── mod.rs # API module exports │ │ └── symmetric.rs # Public symmetric encryption API │ ├── core/ # Internal implementation modules │ │ ├── error.rs # Error types and conversions │ │ ├── keypair.rs # Core keypair implementation │ │ ├── mod.rs # Core module exports │ │ └── symmetric.rs # Core symmetric encryption implementation │ ├── tests/ # Test modules │ │ ├── keypair_tests.rs # Tests for keypair functionality │ │ ├── mod.rs # Test module exports │ │ └── symmetric_tests.rs # Tests for symmetric encryption │ └── lib.rs # Main entry point, exports WASM functions ├── www/ │ ├── index.html # Example HTML page │ ├── server.js # Simple HTTP server for testing │ └── js/ │ └── index.js # JavaScript code to load and use the WebAssembly module ├── Cargo.toml # Rust package configuration ├── start.sh # Script to build and run the example └── README.md # This file ``` ## Running the Example The easiest way to run the example is to use the provided start script: ```bash ./start.sh ``` This script will: 1. Build the WebAssembly module using wasm-pack 2. Start a local HTTP server Then open your browser and navigate to http://localhost:8080. ## Building Manually If you prefer to build and run the example manually: 1. Build the WebAssembly module: ```bash wasm-pack build --target web ``` 2. Start the local server: ```bash node www/server.js ``` 3. Open your browser and navigate to http://localhost:8080. ## Running Tests To run the tests: ```bash cargo test ``` ## API Reference ### Keypair Operations ```javascript // Initialize a new keypair const result = await wasm.keypair_new(); if (result === 0) { console.log("Keypair initialized successfully"); } // Get the public key const pubKey = await wasm.keypair_pub_key(); // Sign a message const message = new TextEncoder().encode("Hello, world!"); const signature = await wasm.keypair_sign(message); // Verify a signature const isValid = await wasm.keypair_verify(message, signature); console.log("Signature valid:", isValid); ``` ### Symmetric Encryption ```javascript // Generate a symmetric key const key = wasm.generate_symmetric_key(); // Encrypt a message const message = new TextEncoder().encode("Secret message"); const ciphertext = await wasm.encrypt_symmetric(key, message); // Decrypt a message const decrypted = await wasm.decrypt_symmetric(key, ciphertext); const decryptedText = new TextDecoder().decode(decrypted); console.log("Decrypted:", decryptedText); ``` ## Security Considerations - The keypair is stored in memory and is not persisted between page reloads. - The symmetric encryption uses ChaCha20Poly1305, which provides authenticated encryption. - The nonce for symmetric encryption is generated randomly and appended to the ciphertext. ## License This project is licensed under the MIT License - see the LICENSE file for details.