src | ||
www | ||
.gitignore | ||
Cargo.toml | ||
implementation_plan.md | ||
LICENSE | ||
README.md | ||
start.sh |
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 (1.70.0 or later)
- wasm-pack (0.10.0 or later)
- Node.js (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:
./start.sh
This script will:
- Build the WebAssembly module using wasm-pack
- 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:
- Build the WebAssembly module:
wasm-pack build --target web
- Start the local server:
node www/server.js
- Open your browser and navigate to http://localhost:8080.
Running Tests
To run the tests:
cargo test
API Reference
Keypair Operations
// 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
// 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.