htauth

Crates.iohtauth
lib.rshtauth
version0.1.2
created_at2026-01-17 16:27:19.029451+00
updated_at2026-01-17 16:54:15.247471+00
descriptionA library for managing htpasswd files
homepage
repositoryhttps://github.com/mexus/htauth
max_upload_size
id2050741
size55,145
Denis (mexus)

documentation

https://docs.rs/htauth

README

htauth

A Rust library for managing htpasswd files with support for bcrypt, SHA-256, SHA-512, and APR1-MD5 password hashing algorithms.

Crates.io Documentation License

API Documentation | Repository

Usage

use htauth::{Htpasswd, HashAlgorithm};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Open or create an htpasswd file
    let mut htpasswd = Htpasswd::open(".htpasswd")?;

    // Add a new user with bcrypt hashing
    htpasswd.add_user("alice", "password123", HashAlgorithm::Bcrypt)?;

    // Verify credentials
    if htpasswd.verify_user("alice", "password123")? {
        println!("Authentication successful");
    }

    // List all usernames
    for username in htpasswd.list_users() {
        println!("{}", username);
    }

    // Remove a user
    htpasswd.delete_user("alice")?;

    // Persist changes to disk
    htpasswd.save()?;

    Ok(())
}

Supported Hash Algorithms

Algorithm Identifier Format
bcrypt Bcrypt $2y$ / $2b$
SHA-256 Sha256 $5$
SHA-512 Sha512 $6$
APR1-MD5 Apr1 $apr1$

The library can read and verify all formats. Use detect_algorithm to determine the algorithm of an existing hash.

API Overview

  • Htpasswd — Main type for loading, modifying, and saving password files
  • HashAlgorithm — Enum representing supported hash algorithms
  • hash_password / verify_password — Standalone functions for password hashing
  • detect_algorithm — Detect algorithm from an existing hash string

Error Handling

The library uses snafu for structured error handling. Error types are re-exported at the crate level:

  • HashError — password hashing errors
  • HtpasswdError — file parsing and I/O errors
  • Apr1Md5Error — APR1-MD5 specific errors

Apache Compatibility

Files generated by this library are fully compatible with Apache's htpasswd tool and vice versa.

CLI

For a command-line interface, see the htauth-cli crate.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Commit count: 22

cargo fmt