arcanum_hash/lib.rs
1//! # Arcanum Hash
2//!
3//! Cryptographic hash functions and key derivation for the Arcanum engine.
4//!
5//! ## Hash Function Selection
6//!
7//! ### Performance Comparison (4KB message, typical desktop CPU)
8//!
9//! | Algorithm | Throughput | Security | Use Case |
10//! |-----------|------------|----------|----------|
11//! | **BLAKE3** | **5.2 GiB/s** | 256-bit | **Recommended default** |
12//! | SHA-256 | 1.7 GiB/s | 256-bit | TLS, Bitcoin, compatibility |
13//! | SHA-512 | 1.9 GiB/s | 512-bit | Ed25519, larger output |
14//! | SHA-3-256 | 0.8 GiB/s | 256-bit | NIST compliance |
15//! | BLAKE2b | 1.2 GiB/s | 512-bit | Legacy BLAKE support |
16//!
17//! ### Recommendations
18//!
19//! **Use BLAKE3 ([`Blake3`]) for:**
20//! - Content addressing / deduplication (file systems, object stores)
21//! - File integrity checking (checksums, manifests)
22//! - Key derivation with [`Blake3::derive_key`]
23//! - Keyed hashing / MAC with [`Blake3::keyed_hash`]
24//! - Any new application without legacy compatibility requirements
25//! - Streaming large files (BLAKE3 is parallelizable)
26//!
27//! **Use SHA-256 ([`Sha256`]) for:**
28//! - TLS / X.509 certificates
29//! - Bitcoin / cryptocurrency protocols
30//! - Compatibility with existing systems (HMAC, JWT, etc.)
31//! - NIST-approved algorithm requirements
32//!
33//! **Use SHA-3 ([`Sha3_256`]) for:**
34//! - Government/compliance requiring NIST SP 800-185
35//! - Defense-in-depth (different construction than SHA-2)
36//!
37//! ### Type Alias
38//!
39//! For convenience, [`PreferredHasher`] is an alias for [`Blake3`]:
40//!
41//! ```ignore
42//! use arcanum_hash::PreferredHasher;
43//!
44//! let hash = PreferredHasher::hash(b"data");
45//! ```
46//!
47//! ## Hash Functions
48//!
49//! - **BLAKE3**: Ultra-fast, parallelizable, recommended default
50//! - **SHA-2**: SHA-256, SHA-384, SHA-512 (NIST standard)
51//! - **SHA-3**: SHA3-256, SHA3-512, SHAKE128, SHAKE256 (Keccak)
52//! - **Blake2**: Blake2b, Blake2s (fast, secure, legacy)
53//!
54//! ## Key Derivation Functions
55//!
56//! - **Argon2id**: Password hashing (PHC winner, recommended)
57//! - **BLAKE3 KDF**: Fast key derivation from high-entropy input
58//! - **HKDF**: Key derivation from high-entropy input (RFC 5869)
59//! - **scrypt**: Memory-hard password hashing
60//! - **PBKDF2**: Legacy password hashing
61//!
62//! ## Message Authentication Codes
63//!
64//! - **BLAKE3 keyed**: Fast keyed hashing built into BLAKE3
65//! - **HMAC**: Hash-based MAC (with any hash function)
66//!
67//! ## Example
68//!
69//! ```ignore
70//! use arcanum_hash::prelude::*;
71//!
72//! // Recommended: Use BLAKE3 for new applications
73//! let hash = Blake3::hash(b"hello world");
74//!
75//! // BLAKE3 keyed hash (MAC)
76//! let key = [0u8; 32];
77//! let mac = Blake3::keyed_hash(&key, b"message");
78//!
79//! // BLAKE3 key derivation
80//! let derived = Blake3::derive_key("my-app v1 encryption key", b"context data");
81//!
82//! // Incremental hashing for large data
83//! let mut hasher = Blake3::new();
84//! hasher.update(b"hello ");
85//! hasher.update(b"world");
86//! let hash = hasher.finalize();
87//!
88//! // SHA-256 for compatibility
89//! let sha_hash = Sha256::hash(b"hello world");
90//!
91//! // Password hashing (always use Argon2id)
92//! let hash = Argon2::hash_password(b"password", &Argon2Params::default())?;
93//! assert!(Argon2::verify_password(b"password", &hash)?);
94//!
95//! // HKDF for protocol key derivation
96//! let key = Hkdf::<Sha256>::derive(ikm, salt, info, 32)?;
97//! ```
98//!
99//! ## Security Notes
100//!
101//! - **BLAKE3** is cryptographically secure with 256-bit security level
102//! - All hash functions in this crate are collision-resistant and pre-image resistant
103//! - For password hashing, **always** use Argon2id, never raw hash functions
104//! - BLAKE3 keyed hashing is a proper MAC construction, not just `H(key || message)`
105
106#![deny(unsafe_code)]
107#![warn(missing_docs, rust_2018_idioms)]
108#![allow(unused_variables, dead_code)]
109
110#[cfg(feature = "sha2")]
111pub mod sha2_impl;
112
113#[cfg(feature = "sha3")]
114pub mod sha3_impl;
115
116#[cfg(feature = "blake2")]
117pub mod blake2_impl;
118
119#[cfg(feature = "blake3")]
120pub mod blake3_impl;
121
122#[cfg(feature = "argon2")]
123pub mod argon2_impl;
124
125#[cfg(feature = "hkdf")]
126pub mod hkdf_impl;
127
128#[cfg(feature = "scrypt")]
129pub mod scrypt_impl;
130
131#[cfg(feature = "mac")]
132pub mod hmac_impl;
133
134mod traits;
135
136pub use traits::{HashOutput, Hasher, KeyDerivation, PasswordHash};
137
138#[cfg(feature = "sha2")]
139pub use sha2_impl::{Sha256, Sha384, Sha512};
140
141#[cfg(feature = "sha3")]
142pub use sha3_impl::{Sha3_256, Sha3_512, Shake128, Shake256};
143
144#[cfg(feature = "blake2")]
145pub use blake2_impl::{Blake2b, Blake2s};
146
147#[cfg(feature = "blake3")]
148pub use blake3_impl::Blake3;
149
150/// Preferred hash function for new applications.
151///
152/// This is an alias for [`Blake3`], which provides:
153/// - 4-5x faster hashing than SHA-256
154/// - Built-in keyed hashing (MAC)
155/// - Built-in key derivation function
156/// - Parallelizable for large inputs
157/// - 256-bit security level
158///
159/// Use SHA-256 instead only when compatibility with existing systems is required.
160#[cfg(feature = "blake3")]
161pub type PreferredHasher = Blake3;
162
163#[cfg(feature = "argon2")]
164pub use argon2_impl::{Argon2, Argon2Params};
165
166#[cfg(feature = "hkdf")]
167pub use hkdf_impl::Hkdf;
168
169#[cfg(feature = "scrypt")]
170pub use scrypt_impl::{Scrypt, ScryptParams};
171
172#[cfg(feature = "mac")]
173pub use hmac_impl::Hmac;
174
175/// Prelude for convenient imports.
176pub mod prelude {
177 pub use crate::traits::{HashOutput, Hasher, KeyDerivation, PasswordHash};
178
179 #[cfg(feature = "sha2")]
180 pub use crate::sha2_impl::{Sha256, Sha384, Sha512};
181
182 #[cfg(feature = "sha3")]
183 pub use crate::sha3_impl::{Sha3_256, Sha3_512};
184
185 #[cfg(feature = "blake3")]
186 pub use crate::blake3_impl::Blake3;
187
188 #[cfg(feature = "blake3")]
189 pub use crate::PreferredHasher;
190
191 #[cfg(feature = "argon2")]
192 pub use crate::argon2_impl::{Argon2, Argon2Params};
193
194 #[cfg(feature = "hkdf")]
195 pub use crate::hkdf_impl::Hkdf;
196}