| Crates.io | dcrypt-algorithms |
| lib.rs | dcrypt-algorithms |
| version | 1.2.2 |
| created_at | 2025-07-24 20:36:32.522189+00 |
| updated_at | 2025-12-10 20:21:46.33819+00 |
| description | Cryptographic primitives for the dcrypt library |
| homepage | |
| repository | https://github.com/ioi-foundation/dcrypt |
| max_upload_size | |
| id | 1766901 |
| size | 1,672,859 |
dcrypt-algorithms is a comprehensive, high-assurance cryptographic library for Rust, providing a wide array of primitives with a strong focus on security, correctness, and type-safety.
This crate serves as the core cryptographic engine for the dcrypt ecosystem, implementing algorithms designed to be resistant to side-channel attacks through constant-time execution and secure memory handling.
This library provides low-level cryptographic implementations intended to be used through the higher-level APIs of the dcrypt suite. It is built with the following principles:
std and no_std environments (with alloc), making it suitable for a wide range of applications from servers to embedded systems.The crate provides a broad range of cryptographic primitives, categorized as follows:
Argon2id, Argon2i, and Argon2d variants.secp256k1.sect283k1.This library is written with a security-first mindset.
SecretBuffer, Zeroizing) that automatically zero their contents when they go out of scope, preventing accidental leakage.SymmetricKey<Aes128, 16> cannot be accidentally used with a ChaCha20 cipher, preventing API misuse.Here are a few examples of how to use the primitives in this crate.
use dcrypt::algorithms::aead::ChaCha20Poly1305;
use dcrypt::algorithms::types::Nonce;
// Create a key and nonce
let key = [0x42; 32];
let nonce_data = [0x24; 12];
let nonce = Nonce::<12>::new(nonce_data);
// Create a cipher instance
let cipher = ChaCha20Poly1305::new(&key);
// Encrypt plaintext with associated data
let plaintext = b"Hello, secure world!";
let aad = b"metadata";
let ciphertext = cipher.encrypt(&nonce, plaintext, Some(aad)).unwrap();
// Decrypt
let decrypted = cipher.decrypt(&nonce, &ciphertext, Some(aad)).unwrap();
assert_eq!(decrypted, plaintext);
use dcrypt::algorithms::hash::{Sha256, HashFunction};
// One-shot hashing
let digest = Sha256::digest(b"some data").unwrap();
println!("SHA-256 Digest: {}", digest.to_hex());
// Incremental hashing
let mut hasher = Sha256::new();
hasher.update(b"some ").unwrap();
hasher.update(b"data").unwrap();
let digest2 = hasher.finalize().unwrap();
assert_eq!(digest, digest2);
use dcrypt::algorithms::ec::p256;
use rand::rngs::OsRng;
// 1. Alice generates a keypair.
let (alice_sk, alice_pk) = p256::generate_keypair(&mut OsRng).unwrap();
// 2. Bob generates a keypair.
let (bob_sk, bob_pk) = p256::generate_keypair(&mut OsRng).unwrap();
// 3. Alice and Bob compute their shared secrets.
let alice_shared_secret = p256::scalar_mult(&alice_sk, &bob_pk).unwrap();
let bob_shared_secret = p256::scalar_mult(&bob_sk, &alice_pk).unwrap();
// Both secrets will be the same elliptic curve point.
assert_eq!(alice_shared_secret, bob_shared_secret);
// They can then use a KDF on the x-coordinate to derive a symmetric key.
let key_material = alice_shared_secret.x_coordinate_bytes();
let derived_key = p256::kdf_hkdf_sha256_for_ecdh_kem(&key_material, Some(b"ecdh-example")).unwrap();
no_std SupportThis crate supports no_std environments by disabling the default std feature. Many algorithms require an allocator, which can be enabled with the alloc feature.
[dependencies.dcrypt-algorithms]
version = "0.12.0-beta.1"
default-features = false
features = ["alloc", "hash", "mac", "aead"] # Enable desired algorithm modules
This crate includes a comprehensive benchmark suite using criterion. To run the benchmarks:
cargo bench
HTML reports will be generated in the target/criterion/report directory.
This crate uses feature flags to control which algorithm modules are compiled.
std: Enables functionality that requires the standard library. Enables alloc automatically.alloc: Enables functionality that requires a memory allocator (like Vec and Box).hash: Enables all hash function modules (SHA-2, SHA-3, BLAKE2, etc.).xof: Enables extendable-output functions (SHAKE, BLAKE3). Requires alloc.aead: Enables authenticated encryption ciphers (AES-GCM, ChaCha20-Poly1305). Requires alloc.block: Enables block ciphers (AES) and modes (CBC, CTR).kdf: Enables key derivation functions (Argon2, PBKDF2, HKDF). Requires alloc.mac: Enables message authentication codes (HMAC, Poly1305).stream: Enables stream ciphers (ChaCha20).ec: Enables all elliptic curve cryptography. Requires alloc.By default, std, xof, and ec are enabled.
This project is licensed under the APACHE 2.0 License.