Crates.io | crypto-async-rs |
lib.rs | crypto-async-rs |
version | 0.1.3 |
created_at | 2025-09-10 12:37:49.736123+00 |
updated_at | 2025-09-11 16:41:33.138491+00 |
description | High-performance pure Rust cryptographic library with async streaming support |
homepage | https://gitlab.com/Efimster/sha |
repository | https://gitlab.com/Efimster/sha |
max_upload_size | |
id | 1832467 |
size | 632,423 |
A high-performance, pure Rust cryptographic library providing both synchronous and asynchronous implementations of essential cryptographic primitives. This library focuses on streaming operations and async I/O for optimal performance in modern Rust applications.
futures
for async support)This library is built with pure Rust and has virtually zero external dependencies (only futures
for async support). This provides several critical advantages:
use crypto_async_rs::aes_gcm_async::{gcm_aes_encrypt_async, AesGcmAsyncError};
use crypto_async_rs::aes_gcm::GcmBlockMulEnhancement;
use futures::io::Cursor;
#[tokio::main]
async fn main() -> Result<(), AesGcmAsyncError> {
let key = [0u8; 32]; // 256-bit key
let iv = [0u8; 12]; // 96-bit IV
let plaintext = b"Hello, World!";
let aad = b"additional data"; // Additional authenticated data
// Create multiplication function for performance optimization
let mul_fn = GcmBlockMulEnhancement::None.to_mul_fn(&key);
// Encrypt data
let mut ciphertext = Vec::new();
let reader = Cursor::new(plaintext);
let mut writer = Cursor::new(&mut ciphertext);
let tag = gcm_aes_encrypt_async(&key, &iv, reader, &mut writer, aad, &mul_fn).await?;
println!("Ciphertext: {:?}", ciphertext);
println!("Tag: {:?}", tag);
Ok(())
}
use crypto_async_rs::cha_cha_poly_async::cha_cha_20_aead_encrypt;
use futures::io::Cursor;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let key = [0u8; 32]; // 256-bit key
let nonce = [0u8; 12]; // 96-bit nonce
let plaintext = b"Hello, World!";
let aad = b"additional data"; // Additional authenticated data
// Encrypt data
let mut ciphertext = Vec::new();
let reader = Cursor::new(plaintext);
let mut writer = Cursor::new(&mut ciphertext);
let tag = cha_cha_20_aead_encrypt(aad, &key, nonce, reader, &mut writer).await?;
println!("Ciphertext: {:?}", ciphertext);
println!("Tag: {:?}", tag);
Ok(())
}
use crypto_async_rs::ecdh_x25519::{x25519, U_COORDINATE, X25519Error};
use rand::Rng;
fn main() -> Result<(), X25519Error> {
// Generate random private keys
let mut rng = rand::thread_rng();
let alice_private: [u8; 32] = rng.gen();
let bob_private: [u8; 32] = rng.gen();
// Compute public keys
let alice_public = x25519(alice_private, U_COORDINATE)?;
let bob_public = x25519(bob_private, U_COORDINATE)?;
// Perform key exchange
let alice_shared = x25519(alice_private, bob_public)?;
let bob_shared = x25519(bob_private, alice_public)?;
// Both parties now have the same shared secret
assert_eq!(alice_shared, bob_shared);
println!("Key exchange successful!");
Ok(())
}
use crypto_async_rs::sha512::encode_async; // Best performance: 393 MiB/s
use futures::io::Cursor;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = b"Hello, World!";
// Hash data asynchronously with <2% overhead
let reader = Cursor::new(data);
let hash = encode_async(reader).await?;
println!("SHA512: {:02x?}", hash);
Ok(())
}
Algorithm Selection Guide:
The library includes comprehensive benchmarks to evaluate performance:
# Run all benchmarks
cargo bench
# Run specific algorithm benchmarks
cargo bench --bench cha_cha_poly_async
cargo bench --bench aes_gcm_async
cargo bench --bench ecdh_x25519 --features bench
cargo bench --bench sha_async
# Run with test mode (faster, for verification)
cargo bench --bench cha_cha_poly_async -- --test
# Run specific benchmark groups
cargo bench --bench cha_cha_poly_async -- cha_cha_20_encrypt
cargo bench --bench aes_gcm_async -- aes_gcm_encrypt_128
cargo bench --bench ecdh_x25519 --features bench -- key_exchange
Algorithm | Data Size | Throughput | Performance Rating |
---|---|---|---|
ChaCha20-Poly1305 | 64KB | 359 MiB/s | โญโญโญโญโญ |
AES-256-GCM | 64KB | 253 MiB/s | โญโญโญโญโญ |
X25519 ECDH | N/A | 4,070 ops/sec | โญโญโญโญโญ |
SHA512 | 64KB | 393 MiB/s | โญโญโญโญโญ |
SHA384 | 64KB | 380 MiB/s | โญโญโญโญโญ |
SHA256 | 64KB | 252 MiB/s | โญโญโญโญ |
SHA224 | 64KB | 252 MiB/s | โญโญโญโญ |
SHA1 | 64KB | 258 MiB/s | โญโญโญโญ |
For comprehensive performance analysis and hardware-specific comparisons, see:
src/
โโโ aes.rs # AES block cipher implementation
โโโ aes_gcm.rs # AES-GCM synchronous implementation
โโโ aes_gcm_async.rs # AES-GCM async streaming implementation
โโโ cha_cha_poly.rs # ChaCha20-Poly1305 synchronous implementation
โโโ cha_cha_poly_async.rs # ChaCha20-Poly1305 async streaming implementation
โโโ ecdh_x25519.rs # X25519 ECDH key exchange
โโโ sha*.rs # SHA family implementations (sync & async)
โโโ hmac.rs # HMAC implementation
โโโ hkdf.rs # HKDF key derivation
# Run all tests
cargo test
# Run tests with specific features
cargo test --features async
cargo test --features bench
# Run benchmarks
cargo bench
# Generate HTML benchmark reports
cargo bench -- --save-baseline main
async
(default): Enables async/streaming operationsbench
: Enables benchmarking featuresContributions are welcome! Please see the benchmark results and analysis for areas that could benefit from optimization.
git clone https://github.com/your-username/crypto-async-rs.git
cd crypto-async-rs
cargo build
cargo test
cargo bench
This project is licensed under the MIT License - see the LICENSE file for details.
This library achieves top 5-10% performance compared to industry-standard cryptographic libraries:
For detailed performance analysis and hardware-specific comparisons, see the comprehensive benchmark documentation in the benches/
directory.
Note: This library is designed for high-performance applications requiring both security and speed. All implementations follow cryptographic best practices and are suitable for production use.