| Crates.io | browser-crypto |
| lib.rs | browser-crypto |
| version | 0.1.0 |
| created_at | 2025-02-15 15:49:14.336307+00 |
| updated_at | 2025-02-15 15:49:14.336307+00 |
| description | A safe Rust interface to browser-based cryptographic operations using the Web Crypto API |
| homepage | |
| repository | https://github.com/jdrouet/browser-crypto |
| max_upload_size | |
| id | 1556900 |
| size | 42,918 |
A safe Rust interface to browser-based cryptographic operations using the Web Crypto API.
This crate provides a type-safe wrapper around the browser's native cryptographic functionality, making it easier to perform common cryptographic operations in WebAssembly applications.
Add this to your Cargo.toml:
[dependencies]
browser-crypto = "0.1.0"
use browser_crypto::aes256gcm::Aes256Gcm;
use browser_crypto::algorithm::Algorithm;
async fn encrypt_data() -> Result<(), Box<dyn std::error::Error>> {
// Create a new AES-256-GCM instance with a key
let key_bytes = [0u8; 32]; // Replace with your secure key
let cipher = Aes256Gcm::from_key(&key_bytes).await?;
// Generate a random nonce
let nonce = Aes256Gcm::generate_nonce()?;
// Encrypt some data
let data = b"Secret message";
let encrypted = cipher.encrypt(&nonce, data).await?;
// Decrypt the data
let decrypted = cipher.decrypt(&nonce, &encrypted).await?;
assert_eq!(data.to_vec(), decrypted);
Ok(())
}
This crate relies on the browser's implementation of the Web Crypto API, which:
However, users should be aware that:
log-error: Enables console logging of unknown errors (useful for debugging)This crate requires a browser with support for:
Most modern browsers (Chrome, Firefox, Safari, Edge) support these features.
The crate provides detailed error types that map directly to Web Crypto API exceptions:
Error: General Web Crypto API errorsEncryptionError: Encryption-specific errorsDecryptionError: Decryption-specific errorsNonceError: Nonce generation and validation errorsImportKeyError: Key import and format errorsContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under MIT.