RustCrypto Password Hashes
repository·master·Indexed 21 days ago
https://github.com/rustcrypto/password-hashesA collection of pure Rust implementations of password hashing algorithms and key derivation functions. Supported algorithms include Argon2 (d, i, and id variants), Balloon Hash, bcrypt-pbkdf, PBKDF2, scrypt, SHA-crypt, and yescrypt. The collection includes the password-auth crate for high-level authentication interfaces and integrates with the PHC format via the PasswordVerifier trait for flexible password verification across multiple algorithms.
What's inside rustcrypto-password-hashes
- RustCrypto: Password Hashes is a collection of password hashing algorithms (password-based key derivation functions) implemented in pure Rust. It provides a unified way to work with various algorithms through common traits, allowing for flexible password verification.
Overview of SHA-crypt password hash
masterThe
sha-cryptcrate provides a pure Rust implementation of the SHA-crypt password hashing scheme based on SHA-256 or SHA-512. This is a legacy hashing scheme commonly supported by the POSIXcryptC library.When using the Modular Crypt Format (MCF), hashes generated with this algorithm are identified by the following prefixes:
$5$for SHA-256 based hashes.$6$for SHA-512 based hashes.
Overview of Argon2 implementation
masterThe
argon2crate provides a pure Rust implementation of the Argon2 password hashing function, which was the winner of the Password Hashing Competition in 2015. It is a memory-hard key derivation function designed to be resistant to various attacks.It supports three algorithmic variants:
- Argon2d: Maximizes resistance to GPU cracking attacks.
- Argon2i: Optimized to resist side-channel attacks.
- Argon2id: The default hybrid version that combines the properties of both Argon2i and Argon2d.
The implementation supports embedded (
no_std) environments, including those withoutallocsupport.Overview of PBKDF2 implementation
masterThepbkdf2crate provides a pure Rust implementation of the Password-Based Key Derivation Function v2 (PBKDF2) as specified in RFC 2898. It is part of the RustCrypto ecosystem.Overview of Balloon Hash implementation
masterTheballoon-hashcrate provides a pure Rust implementation of the Balloon password hashing function. As specified in the paper "Balloon Hashing: A Memory-Hard Function Providing Provable Protection Against Sequential Attacks", this algorithm is designed to provide memory hardness proven in the random-oracle model, password-independent access, and performance that meets or exceeds existing heuristically secure password-hashing algorithms.Overview of the scrypt crate
masterscryptis a pure Rust implementation of the scrypt key derivation function. It is a sequential memory-hard function designed to be resistant to hardware acceleration (like ASICs) and can be used for both key derivation and password hashing.Overview of the yescrypt crate
masteryescryptis a pure Rust implementation of the yescrypt password-based key derivation function (KDF). It is a variant ofscryptand was a finalist in the Password Hashing Competition. It is widely used by Linux distributions such as Fedora, Debian, Ubuntu, and Arch for system password hashing.⚠️ Security Warning
This implementation has never been independently audited. It is in an early stage of implementation and may contain bugs or incorrect features. Use it at your own risk.
What is bcrypt-pbkdf?
masterbcrypt-pbkdfis a pure Rust implementation of the password-based key derivation function used in OpenSSH. It is a custom derivative of PBKDF2 that replaces standard hash functions (like SHA-2) with a bcrypt-style core based on the Blowfish cipher.It utilizes a modified bcrypt operation called "bhash" which repeatedly mixes the password and salt into Blowfish's internal state. This process is designed to be computationally expensive to thwart brute force attacks, allowing users to control the compute cost via a configurable number of rounds.
How PasswordVerifier and PHC hashes work together
masterThe library uses the PHC (Password Hashing Competition) format for stored hashes. The
password_hash::phc::PasswordHashtype represents the parsed structure of these strings.Algorithms implement the
PasswordVerifier<phc::PasswordHash>trait. This allows a generic verification loop where you can pass aPasswordHashobject to any algorithm implementation to check if a provided password matches the stored hash. This abstraction enables supporting multiple algorithms (like Argon2, PBKDF2, and Scrypt) using a single unified interface.Use the password-auth API for hashing and verification
masterThe
password-authcrate provides a high-level interface for password authentication, abstracting away the complexity of specific hashing algorithms. It uses thepassword-hashcrate internally to support multiple algorithms simultaneously.Core Functions
generate_hash: Generates a new password hash from a provided password. By default, this uses Argon2 with the latest OWASP recommended parameters.verify_password: Verifies a provided password against an existing password hash. It returns an error if the password is incorrect.
Algorithm Support and Features
- Argon2: Supported by default.
- PBKDF2 and scrypt: Supported optionally via crate features.
When multiple algorithms are enabled via features,
generate_hashwill still default to Argon2, butverify_passwordis capable of identifying and verifying hashes created with PBKDF2 or scrypt, allowing for seamless migration or support for legacy hashes in your database.// Note: Actual implementation requires importing the crate // generate_hash(password) -> Result<String, Error> // verify_password(password, hash) -> Result<(), Error>Configure algorithm support via crate features
masterBy default,
password-authonly includes support for Argon2. To support other algorithms, you must enable them in yourCargo.toml:- To support PBKDF2, enable the corresponding feature.
- To support scrypt, enable the corresponding feature.
Enabling these features allows
verify_passwordto work with hashes generated by those algorithms, even thoughgenerate_hashwill continue to default to Argon2.Argon2 Context (Argon2 struct)
masterThe
Argon2<'key>struct is the primary entry point for the crate. It holds the configuration for the hashing process, including:Algorithm: The specific Argon2 variant (e.g., Argon2d, Argon2i, Argon2id).Version: The Argon2 version to use.Params: Memory cost, time cost, parallelism, and output length.secret: An optional byte slice used as a 'pepper'.
Construction
Argon2::default(): Uses default algorithm, version, and parameters.Argon2::new(algorithm, version, params): Creates a context with specific settings.Argon2::new_with_secret(secret, algorithm, version, params): Creates a context including a pepper.