Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AeadCore::generate_nonce in rustdoc examples #466

Merged
merged 1 commit into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions aes-gcm-siv/src/lib.rs
Expand Up @@ -15,15 +15,15 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes_gcm_siv::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! Aes256GcmSiv, Nonce // Or `Aes128GcmSiv`
//! };
//!
//! let key = Aes256GcmSiv::generate_key(&mut OsRng);
//! let cipher = Aes256GcmSiv::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 96-bits; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = Aes256GcmSiv::generate_nonce(&mut OsRng); // 96-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down Expand Up @@ -62,7 +62,7 @@
//! let cipher = Aes256GcmSiv::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 96-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag tag
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
Expand Down
18 changes: 9 additions & 9 deletions aes-gcm/src/lib.rs
Expand Up @@ -16,15 +16,15 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes_gcm::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! Aes256Gcm, Nonce // Or `Aes128Gcm`
//! };
//!
//! let key = Aes256Gcm::generate_key(&mut OsRng);
//! let cipher = Aes256Gcm::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 96-bits; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = Aes256Gcm::generate_nonce(&mut OsRng); // 96-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down Expand Up @@ -55,25 +55,25 @@
)]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes_gcm::{
//! aead::{AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! aead::{AeadCore, AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! Aes256Gcm, Nonce, // Or `Aes128Gcm`
//! };
//!
//! let key = Aes256Gcm::generate_key(&mut OsRng);
//! let cipher = Aes256Gcm::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 96-bits; unique per message
//! let nonce = Aes256Gcm::generate_nonce(&mut OsRng); // 96-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag tag
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(&buffer, b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
//! assert_eq!(&buffer, b"plaintext message");
//! # Ok(())
//! # }
Expand Down
18 changes: 9 additions & 9 deletions aes-siv/src/lib.rs
Expand Up @@ -15,15 +15,15 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes_siv::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! Aes256SivAead, Nonce // Or `Aes128SivAead`
//! };
//!
//! let key = Aes256SivAead::generate_key(&mut OsRng);
//! let cipher = Aes256SivAead::new(&key);
//! let nonce = Nonce::from_slice(b"any unique nonce"); // 128-bits; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = Aes256SivAead::generate_nonce(&mut OsRng); // 128-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down Expand Up @@ -54,25 +54,25 @@
)]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes_siv::{
//! aead::{AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! aead::{AeadCore, AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! Aes256SivAead, Nonce, // Or `Aes128SivAead`
//! };
//!
//! let key = Aes256SivAead::generate_key(&mut OsRng);
//! let cipher = Aes256SivAead::new(&key);
//! let nonce = Nonce::from_slice(b"any unique nonce"); // 128-bits; unique per message
//! let nonce = Aes256SivAead::generate_nonce(&mut OsRng); // 128-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag tag
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(&buffer, b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
//! assert_eq!(&buffer, b"plaintext message");
//! # Ok(())
//! # }
Expand Down
8 changes: 4 additions & 4 deletions ccm/src/lib.rs
Expand Up @@ -16,7 +16,7 @@
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes::Aes256;
//! use ccm::{
//! aead::{Aead, KeyInit, OsRng, generic_array::GenericArray},
//! aead::{Aead, AeadCore, KeyInit, OsRng, generic_array::GenericArray},
//! consts::{U10, U13},
//! Ccm,
//! };
Expand All @@ -26,9 +26,9 @@
//!
//! let key = Aes256Ccm::generate_key(&mut OsRng);
//! let cipher = Aes256Ccm::new(&key);
//! let nonce = GenericArray::from_slice(b"unique nonce."); // 13-bytes; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = Aes256Ccm::generate_nonce(&mut OsRng); // 13-bytes; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down
26 changes: 13 additions & 13 deletions chacha20poly1305/src/lib.rs
Expand Up @@ -26,15 +26,15 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use chacha20poly1305::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! ChaCha20Poly1305, Nonce
//! };
//!
//! let key = ChaCha20Poly1305::generate_key(&mut OsRng);
//! let cipher = ChaCha20Poly1305::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 12-bytes; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng); // 96-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down Expand Up @@ -65,25 +65,25 @@
)]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use chacha20poly1305::{
//! aead::{AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! aead::{AeadCore, AeadInPlace, KeyInit, OsRng, heapless::Vec},
//! ChaCha20Poly1305, Nonce,
//! };
//!
//! let key = ChaCha20Poly1305::generate_key(&mut OsRng);
//! let cipher = ChaCha20Poly1305::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce"); // 12-bytes; unique per message
//! let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng); // 96-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag tag
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Note: buffer needs 16-bytes overhead for auth tag
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(&buffer, b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(nonce, b"", &mut buffer)?;
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
//! assert_eq!(&buffer, b"plaintext message");
//! # Ok(())
//! # }
Expand Down Expand Up @@ -122,15 +122,15 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use chacha20poly1305::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! XChaCha20Poly1305, XNonce
//! };
//!
//! let key = XChaCha20Poly1305::generate_key(&mut OsRng);
//! let cipher = XChaCha20Poly1305::new(&key);
//! let nonce = XNonce::from_slice(b"extra long unique nonce!"); // 24-bytes; unique
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = XChaCha20Poly1305::generate_nonce(&mut OsRng); // 192-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand Down
45 changes: 22 additions & 23 deletions deoxys/src/lib.rs
Expand Up @@ -12,16 +12,16 @@
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use deoxys::{
//! aead::{Aead, KeyInit, OsRng},
//! aead::{Aead, AeadCore, KeyInit, OsRng},
//! DeoxysII256, // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//! Nonce // Or `Aes128Gcm`
//! };
//!
//! let key = DeoxysII256::generate_key(&mut OsRng);
//! let cipher = DeoxysII256::new(&key);
//! let nonce = Nonce::from_slice(b"unique nonce123"); // 64-bits for Deoxys-I or 120-bits for Deoxys-II; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = DeoxysII256::generate_nonce(&mut OsRng); // 120-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand All @@ -30,28 +30,28 @@
//! ## Usage with AAD
//! Deoxys can authenticate additionnal data that is not encrypted alongside with the ciphertext.
//! ```
//! use deoxys::{DeoxysII256, Key, Nonce}; // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//! use deoxys::aead::{Aead, KeyInit, Payload};
//! use deoxys::{DeoxysII256, Nonce}; // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//! use deoxys::aead::{Aead, AeadCore, KeyInit, Payload, OsRng};
//!
//! let key = Key::<DeoxysII256>::from_slice(b"an example very very secret key.");
//! let cipher = DeoxysII256::new(key);
//! let key = DeoxysII256::generate_key(&mut OsRng);
//! let cipher = DeoxysII256::new(&key);
//!
//! let nonce = Nonce::from_slice(b"unique nonce123"); // 64-bits for Deoxys-I or 120-bits for Deoxys-II; unique per message
//! let nonce = DeoxysII256::generate_nonce(&mut OsRng); // 120-bits; unique per message
//!
//!let payload = Payload {
//! let payload = Payload {
//! msg: &b"this will be encrypted".as_ref(),
//! aad: &b"this will NOT be encrypted, but will be authenticated".as_ref(),
//!};
//! };
//!
//! let ciphertext = cipher.encrypt(nonce, payload)
//! let ciphertext = cipher.encrypt(&nonce, payload)
//! .expect("encryption failure!"); // NOTE: handle this error to avoid panics!
//!
//!let payload = Payload {
//! let payload = Payload {
//! msg: &ciphertext,
//! aad: &b"this will NOT be encrypted, but will be authenticated".as_ref(),
//!};
//! };
//!
//! let plaintext = cipher.decrypt(nonce, payload)
//! let plaintext = cipher.decrypt(&nonce, payload)
//! .expect("decryption failure!"); // NOTE: handle this error to avoid panics!
//!
//! assert_eq!(&plaintext, b"this will be encrypted");
Expand All @@ -75,26 +75,25 @@
//! ```
//! # #[cfg(feature = "heapless")]
//! # {
//! use deoxys::{DeoxysII256, Key, Nonce}; // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//! use deoxys::aead::{AeadInPlace, KeyInit};
//! use deoxys::aead::heapless::Vec;
//! use deoxys::{DeoxysII256, Nonce}; // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//! use deoxys::aead::{AeadCore, AeadInPlace, KeyInit, OsRng, heapless::Vec};
//!
//! let key = Key::<DeoxysII256>::from_slice(b"an example very very secret key.");
//! let cipher = DeoxysII256::new(key);
//! let key = DeoxysII256::generate_key(&mut OsRng);
//! let cipher = DeoxysII256::new(&key);
//!
//! let nonce = Nonce::from_slice(b"unique nonce123"); // 64-bits for Deoxys-I or 120-bits for Deoxys-II; unique per message
//! let nonce = DeoxysII256::generate_nonce(&mut OsRng); // 120-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new(); // Buffer needs 16-bytes overhead for tag
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(nonce, b"", &mut buffer).expect("encryption failure!");
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer).expect("encryption failure!");
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(&buffer, b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(nonce, b"", &mut buffer).expect("decryption failure!");
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer).expect("decryption failure!");
//! assert_eq!(&buffer, b"plaintext message");
//! # }
//! ```
Expand Down
27 changes: 16 additions & 11 deletions eax/src/lib.rs
Expand Up @@ -17,17 +17,17 @@
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use aes::Aes256;
//! use eax::{
//! aead::{Aead, KeyInit, OsRng, generic_array::GenericArray},
//! aead::{Aead, AeadCore, KeyInit, OsRng, generic_array::GenericArray},
//! Eax, Nonce
//! };
//!
//! pub type Aes256Eax = Eax<Aes256>;
//!
//! let key = Aes256Eax::generate_key(&mut OsRng);
//! let cipher = Aes256Eax::new(&key);
//! let nonce = GenericArray::from_slice(b"my unique nonces"); // 128-bits; unique per message
//! let ciphertext = cipher.encrypt(nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?;
//! let nonce = Aes256Eax::generate_nonce(&mut OsRng); // 128-bits; unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
Expand All @@ -53,25 +53,30 @@
//! # {
//! use aes::Aes256;
//! use eax::Eax;
//! use eax::aead::{AeadInPlace, KeyInit, generic_array::GenericArray};
//! use eax::aead::heapless::Vec;
//! use eax::aead::{
//! generic_array::GenericArray,
//! heapless::Vec,
//! AeadCore, AeadInPlace, KeyInit, OsRng
//! };
//!
//! let key = GenericArray::from_slice(b"an example very very secret key.");
//! let cipher = Eax::<Aes256>::new(key);
//! pub type Aes256Eax = Eax<Aes256>;
//!
//! let nonce = GenericArray::from_slice(b"my unique nonces"); // 128-bits; unique per message
//! let key = Aes256Eax::generate_key(&mut OsRng);
//! let cipher = Aes256Eax::new(&key);
//!
//! let nonce = Aes256Eax::generate_nonce(&mut OsRng); // 128-bits; unique per message
//!
//! let mut buffer: Vec<u8, 128> = Vec::new();
//! buffer.extend_from_slice(b"plaintext message");
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(nonce, b"", &mut buffer).expect("encryption failure!");
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer).expect("encryption failure!");
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(&buffer, b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(nonce, b"", &mut buffer).expect("decryption failure!");
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer).expect("decryption failure!");
//! assert_eq!(&buffer, b"plaintext message");
//! # }
//! ```
Expand Down