Skip to content

Commit

Permalink
argon2: add a peppering example to the docs (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
sp1ff authored Feb 14, 2025
1 parent a4cfe22 commit 510a3a6
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,58 @@
//! # }
//! ```
//!
//! To [pepper] as well as salt your passwords:
//!
//! [pepper]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#peppering
//!
#![cfg_attr(all(feature = "password-hash", feature = "std"), doc = "```")]
#![cfg_attr(
not(all(feature = "password-hash", feature = "std")),
doc = "```ignore"
)]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use argon2::{
//! password_hash::{
//! // `OsRng` requires enabled `std` crate feature
//! rand_core::OsRng,
//! PasswordHash, PasswordHasher, PasswordVerifier, SaltString
//! },
//! Algorithm, Argon2, Params, Version
//! };
//!
//! let password = b"hunter42"; // Bad password; don't actually use!
//! let salt = SaltString::generate(&mut OsRng);
//!
//! // Argon2 with default params (Argon2id v19) and pepper
//! let argon2 = Argon2::new_with_secret(
//! b"secret pepper",
//! Algorithm::default(),
//! Version::default(),
//! Params::default()
//! )
//! .unwrap();
//!
//! // Hash password to PHC string ($argon2id$v=19$...)
//! let password_hash = argon2.hash_password(password, &salt)?.to_string();
//!
//! // Verify password against PHC string.
//! //
//! // NOTE: hash params from `parsed_hash` are used instead of what is configured in the
//! // `Argon2` instance.
//! let parsed_hash = PasswordHash::new(&password_hash)?;
//! let argon2 = Argon2::new_with_secret(
//! b"secret pepper",
//! Algorithm::default(),
//! Version::default(),
//! Params::default(),
//! )
//! .unwrap();
//! let res = argon2.verify_password(password, &parsed_hash);
//! assert!(res.is_ok());
//! # Ok(())
//! # }
//! ```
//!
//! ### Key Derivation
//!
//! This API is useful for transforming a password into cryptographic keys for
Expand Down

0 comments on commit 510a3a6

Please sign in to comment.