Skip to main content

rusty_crypt/
lib.rs

1//! # rusty_crypt
2//!
3//! `rusty_crypt` is a Rust cryptography library providing a simple and safe
4//! interface for AES-256-GCM symmetric encryption and decryption.
5//!
6//! The library focuses on:
7//!
8//! - AES-256-GCM authenticated encryption
9//! - Secure random AES key generation
10//! - Secure random nonce generation
11//! - Error handling with `Result<T, CryptoError>`
12//! - Support for text and binary data encryption
13//!
14//! ## Features
15//!
16//! ### AES-256-GCM encryption
17//!
18//! The library uses AES-256-GCM, which provides:
19//!
20//! - Confidentiality: encrypted data cannot be read without the key
21//! - Integrity: modified ciphertext is detected during decryption
22//! - Authentication: invalid keys or corrupted data cause an error
23//!
24//! ### Secure key generation
25//!
26//! AES keys are generated using the operating system secure random number
27//! generator through the `getrandom` crate.
28//!
29//! AES-256 requires a 32-byte key.
30//!
31//! ### Secure nonce generation
32//!
33//! AES-GCM requires a unique nonce for every encryption operation.
34//!
35//! This library generates secure 12-byte nonces automatically.
36//!
37//! ## Basic example
38//!
39//! ```rust
40//! use base64::{engine::general_purpose, Engine};
41//!
42//! use rusty_crypt::{
43//!     AesGeneratedKey,
44//!     AesGcmGeneratedNonce,
45//!     AesGcmEncrypt,
46//!     AesGcmDecrypt,
47//! };
48//!
49//! fn main() -> Result<(), Box<dyn std::error::Error>> {
50//!
51//!     let key = AesGeneratedKey.generate_key()?;
52//!
53//!     let key_base64 = general_purpose::STANDARD.encode(key);
54//!
55//!     let nonce_generator = AesGcmGeneratedNonce;
56//!
57//!     let encryptor = AesGcmEncrypt::new(&nonce_generator);
58//!
59//!     let decryptor = AesGcmDecrypt;
60//!
61//!     let message = "Hello rusty_crypt";
62//!
63//!     let encrypted = encryptor.encrypt(
64//!         message,
65//!         &key_base64,
66//!     )?;
67//!
68//!     let decrypted = decryptor.decrypt(
69//!         &encrypted,
70//!         &key_base64,
71//!     )?;
72//!
73//!     assert_eq!(message, decrypted);
74//!
75//!     Ok(())
76//! }
77//! ```
78//!
79//! ## Binary data encryption
80//!
81//! `rusty_crypt` also supports encryption of arbitrary binary data such as:
82//!
83//! - Files
84//! - Images
85//! - Documents
86//! - Network payloads
87//!
88//! Use:
89//!
90//! - [`AesGcmEncrypt::encrypt_bytes`] for encryption
91//! - [`AesGcmDecrypt::decrypt_bytes`] for decryption
92//!
93//! ## Error handling
94//!
95//! All cryptographic operations return a [`CryptoError`] instead of
96//! panicking.
97//!
98//! This allows applications to properly handle:
99//!
100//! - Invalid keys
101//! - Invalid Base64 data
102//! - Encryption failures
103//! - Decryption failures
104//! - Random generation failures
105//!
106//! ## Security notes
107//!
108//! - Never reuse an AES-GCM nonce with the same key.
109//! - Never expose AES keys publicly.
110//! - Store generated keys securely.
111//! - Use a strong key management strategy for production applications.
112//!
113//! This library provides low-level cryptographic building blocks.
114//! Applications requiring password-based encryption, key exchange,
115//! or public-key cryptography should implement additional layers.
116
117pub mod aes_gcm_256_decrypt;
118pub mod aes_gcm_256_encrypt;
119pub mod aes_gcm_256_generated_key;
120pub mod aes_gcm_256_generated_nonce;
121pub mod error;
122
123pub use self::aes_gcm_256_decrypt::AesGcmDecrypt;
124pub use self::aes_gcm_256_encrypt::AesGcmEncrypt;
125pub use self::aes_gcm_256_generated_key::AesGeneratedKey;
126pub use self::aes_gcm_256_generated_nonce::AesGcmGeneratedNonce;
127pub use self::error::CryptoError;