# htauth
A Rust library for managing htpasswd files with support for bcrypt, SHA-256, SHA-512, and APR1-MD5 password hashing algorithms.
[](https://crates.io/crates/htauth)
[](https://docs.rs/htauth)
[](https://github.com/mexus/htauth#license)
## Usage
```rust
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
| 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`](https://docs.rs/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`](https://crates.io/crates/htauth-cli) crate.
## License
Licensed under either of [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) or [MIT license](https://opensource.org/licenses/MIT) at your option.