RustCrypto Password Hashes

repository·master·Indexed 21 days ago

https://github.com/rustcrypto/password-hashes

A 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.

Tokens
21.3K
Snippets
63
Records
95
Agent score
73%

What's inside rustcrypto-password-hashes

  1. Overview of RustCrypto Password Hashes

    master
    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.
  2. Overview of SHA-crypt password hash

    master

    The sha-crypt crate 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 POSIX crypt C 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.
  3. Overview of Argon2 implementation

    master

    The argon2 crate 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 without alloc support.

  4. Overview of Balloon Hash implementation

    master
    The balloon-hash crate 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.
  5. Overview of the yescrypt crate

    master

    yescrypt is a pure Rust implementation of the yescrypt password-based key derivation function (KDF). It is a variant of scrypt and 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.

  6. What is bcrypt-pbkdf?

    master

    bcrypt-pbkdf is 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.

  7. How PasswordVerifier and PHC hashes work together

    master

    The library uses the PHC (Password Hashing Competition) format for stored hashes. The password_hash::phc::PasswordHash type represents the parsed structure of these strings.

    Algorithms implement the PasswordVerifier<phc::PasswordHash> trait. This allows a generic verification loop where you can pass a PasswordHash object 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.

  8. Use the password-auth API for hashing and verification

    master

    The password-auth crate provides a high-level interface for password authentication, abstracting away the complexity of specific hashing algorithms. It uses the password-hash crate 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_hash will still default to Argon2, but verify_password is 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>
  9. Configure algorithm support via crate features

    master

    By default, password-auth only includes support for Argon2. To support other algorithms, you must enable them in your Cargo.toml:

    • To support PBKDF2, enable the corresponding feature.
    • To support scrypt, enable the corresponding feature.

    Enabling these features allows verify_password to work with hashes generated by those algorithms, even though generate_hash will continue to default to Argon2.

  10. Argon2 Context (Argon2 struct)

    master

    The 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.