1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! # IronCrypt: A Robust and Simple Cryptography Library for Rust
//!
//! IronCrypt provides a high-level API designed to simplify common cryptographic tasks,
//! with a focus on modern algorithms and secure practices. It can be used both as a
//! command-line tool and as a Rust library integrated into your applications.
//!
//! ## Core Features
//!
//! - **Streaming Encryption:** Efficiently encrypt and decrypt large files and data streams
//! without loading them entirely into memory.
//! - **Hybrid Encryption:** Combines the speed of symmetric encryption (AES-256-GCM)
//! for data with the security of asymmetric encryption (RSA) for key management.
//! - **State-of-the-Art Password Hashing:** Uses Argon2, a modern and resilient algorithm
//! designed to counter GPU-based brute-force attacks.
//! - **Advanced Key Management:** Supports versioning of RSA keys and includes a rotation
//! mechanism to update keys without having to manually re-encrypt everything.
//! - **Flexible Configuration:** Allows fine-tuning of security parameters like RSA key
//! size, Argon2 "costs," and password strength criteria.
//!
//! ## Quick Start
//!
//! ### Example 1: Encrypting and Verifying a Password
//!
//! The example below shows how to use the `IronCrypt` struct to securely hash a password
//! and verify it later.
//!
//! ```rust
//! use ironcrypt::{IronCrypt, IronCryptConfig, DataType, config::KeyManagementConfig};
//! use std::collections::HashMap;
//! use std::error::Error;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! // 1. Use a temporary directory for keys to keep tests isolated.
//! let temp_dir = tempfile::tempdir()?;
//! let key_dir = temp_dir.path().to_str().unwrap();
//!
//! // 2. Configure IronCrypt to use the temporary directory.
//! let mut config = IronCryptConfig::default();
//! let mut data_type_config = HashMap::new();
//! data_type_config.insert(
//! DataType::Generic,
//! KeyManagementConfig {
//! key_directory: key_dir.to_string(),
//! key_version: "v1".to_string(),
//! passphrase: None,
//! },
//! );
//! config.data_type_config = Some(data_type_config);
//!
//! // 3. Initialize IronCrypt.
//! let crypt = IronCrypt::new(config, DataType::Generic).await?;
//!
//! // 4. Encrypt a password.
//! let password = "MySecurePassword123!";
//! let encrypted_json = crypt.encrypt_password(password)?;
//! println!("Encrypted password: {}", encrypted_json);
//!
//! // 5. Verify the password.
//! let is_valid = crypt.verify_password(&encrypted_json, password)?;
//! assert!(is_valid);
//! println!("Password verification successful!");
//!
//! Ok(())
//! }
//! ```
//!
//! ### Example 2: Streaming File Encryption
//!
//! This example shows how to encrypt a data stream (here, an in-memory `Cursor`,
//! but it works the same way with a `File`).
//!
//! ```rust
//! use ironcrypt::{encrypt_stream, decrypt_stream, generate_rsa_keys, PasswordCriteria, Argon2Config, PublicKey, PrivateKey, algorithms::SymmetricAlgorithm};
//! use std::io::Cursor;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // 1. Generate an RSA key pair (in a real application, load them from a file).
//! let (private_key, public_key) = generate_rsa_keys(2048)?;
//!
//! // 2. Prepare the source and destination streams.
//! let original_data = "This is a secret message that will be streamed for encryption.";
//! let mut source = Cursor::new(original_data.as_bytes());
//! let mut encrypted_dest = Cursor::new(Vec::new());
//!
//! // 3. Encrypt the stream.
//! let mut password = "AnotherStrongPassword123!".to_string();
//! let pk_enum = PublicKey::Rsa(public_key);
//! let recipients = vec![(&pk_enum, "v1")];
//! encrypt_stream(
//! &mut source,
//! &mut encrypted_dest,
//! &mut password,
//! recipients,
//! None, // signing_key
//! &PasswordCriteria::default(),
//! Argon2Config::default(),
//! true, // Indicates that the password should be hashed
//! SymmetricAlgorithm::Aes256Gcm,
//! )?;
//!
//! // 4. Go back to the beginning of the encrypted stream to read it.
//! encrypted_dest.set_position(0);
//!
//! // 5. Decrypt the stream.
//! let mut decrypted_dest = Cursor::new(Vec::new());
//! decrypt_stream(
//! &mut encrypted_dest,
//! &mut decrypted_dest,
//! &PrivateKey::Rsa(private_key),
//! "v1",
//! "AnotherStrongPassword123!",
//! None // verifying_key
//! )?;
//!
//! // 6. Verify that the decrypted data matches the original data.
//! let decrypted_data = String::from_utf8(decrypted_dest.into_inner())?;
//! assert_eq!(original_data, decrypted_data);
//! println!("Stream encryption and decryption successful!");
//!
//! Ok(())
//! }
//! ```
//!
//! For more advanced examples, including custom configurations,
//! check out the `examples/` directory of the project.
// --- Modules ---
// --- Public Re-exports ---
// Main configuration
pub use ;
// Key types
pub use ;
// Password criteria
pub use PasswordCriteria;
// Cryptographic standards
pub use CryptoStandard;
// Streaming encryption and decryption functions
pub use ;
pub use ;
/// Contains the parameters for the Argon2 hashing algorithm.
pub use Argon2Config;
/// Struct containing the encrypted data and associated metadata.
pub use EncryptedData;
// Error handling
pub use IronCryptError;
// Password hashing function
pub use hash_password;
// Main library struct
pub use IronCrypt;
// RSA key utilities
pub use ;
// Secret management
pub use vault;
pub use SecretStore;
pub use aws;
pub use azure;
pub use google;
/// Tries to load a public key from a file, attempting to parse it as RSA and then ECC.
/// Tries to load a private key from a file, attempting to parse it as RSA and then ECC.
// Ensure every ```rust``` block in README.md compiles (and runs) under
// `cargo test --doc`. Sketch / framework samples use ```rust,ignore```.
doctest!;