rust-otp is a modern, flexible Rust library for performing the HMAC-based One-time Password Algorithm (HOTP) as per RFC 4226 and the Time-based One-time Password Algorithm (TOTP) as per RFC 6238.
- Modern Builder API: Easy and readable configuration for HOTP and TOTP.
- Async Support: Native
asyncmethods for seamless integration into modern runtimes. - Enhanced Security: Support for SHA1, SHA256, and SHA512.
- Formatted Output: Direct support for zero-padded strings.
- Zero Dependencies (core): Light-weight and secure.
Add this to your Cargo.toml:
[dependencies]
rust-otp = "3.0.0"To enable async support:
rust-otp = { version = "3.0.0", features = ["async"] }use rust_otp::{TOTP, Algorithm};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Standard Google Authenticator configuration (30s step, 6 digits)
let totp = TOTP::builder()
.base32_secret("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")?
.digits(6)
.build()?;
let code = totp.generate_current_formatted()?;
println!("Current TOTP code: {}", code);
Ok(())
}use rust_otp::{HOTP, Algorithm};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let hotp = HOTP::builder()
.base32_secret("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")?
.algorithm(Algorithm::SHA256)
.digits(8)
.build()?;
let code = hotp.generate_formatted(1234);
println!("HOTP code at counter 1234: {}", code);
Ok(())
}#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let totp = TOTP::builder()
.base32_secret("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")?
.build()?;
let code = totp.generate_current_formatted_async().await?;
println!("Async TOTP code: {}", code);
Ok(())
}You can run the provided examples using cargo:
# Run HOTP example
cargo run --example hotp
# Run TOTP example
cargo run --example totp
# Run Async TOTP example
cargo run --example async_totp --features asyncrust-otp is licensed under the MIT license.
The full license is included in this repository in LICENSE.md.