pub mod encryption;
pub mod hashing;
pub mod keys;
pub use encryption::{EncryptionError, decrypt_data, encrypt_data};
pub use hashing::{HashingError, generate_salt, hash_api_key, verify_api_key};
pub use keys::{KeyDerivationError, derive_key, generate_secure_key};
pub use encryption::EncryptedData;
pub use hashing::Salt;
pub fn init() -> Result<(), CryptoError> {
use rand::RngCore;
let mut rng = rand::thread_rng();
let mut test_bytes = [0u8; 32];
rng.fill_bytes(&mut test_bytes);
if test_bytes.iter().all(|&b| b == 0) {
return Err(CryptoError::RandomnessError(
"Failed to generate random bytes".into(),
));
}
Ok(())
}
#[derive(Debug, thiserror::Error)]
pub enum CryptoError {
#[error("Encryption error: {0}")]
Encryption(#[from] EncryptionError),
#[error("Hashing error: {0}")]
Hashing(#[from] HashingError),
#[error("Key derivation error: {0}")]
KeyDerivation(#[from] KeyDerivationError),
#[error("Randomness error: {0}")]
RandomnessError(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_crypto_init() {
assert!(init().is_ok());
}
}