A Rust implementation the [HOTP] and [TOTP] Algorithms.
To use HOTP:
```rust use xotp::hotp::HOTP;
fn getotpwithhotp() { let secret = "secret"; let counter = 0; // Get a HOTP instance with a '&str' secret let hotpstr = HOTP::fromutf8(secret); // Get an otp with the given counter and digit count let otpfromstr = hotpstr.getotp(counter, 6); println!("The otp from hotpstr: {}", otpfromstr);
// Alternatively, get a HOTP instance with a '&[u8]' secret
let hotp_bytes = HOTP::new(secret.as_bytes());
// Get an otp with the given counter and digit count
let otp_from_bytes = hotp_bytes.get_otp(counter, 6);
println!("The otp from hotp_bytes: {}", otp_from_bytes);
} ```
To use TOTP:
```rust use xotp::totp::TOTP; use xotp::util::MacDigest; // Only needed if using a non-SHA1 hash function use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn getotpwithtotp() { let secret = "secret"; let elapsedseconds = SystemTime::now() .durationsince(SystemTime::UNIXEPOCH) .expect("Error getting time") .assecs(); // Get a TOTP instance a '&str' secret and default SHA1 Digest let totpsha1str = TOTP::fromutf8(secret); // Get an otp with the given counter and elapsed seconds let otpsha1 = totpsha1str.getotp(elapsedseconds, 8); println!("The otp from totpsha1str: {}", otpsha1);
// Alternatively get a TOTP instance with a '&[u8]' secret
// and different digest (Sha256 or Sha512)
let totp_sha256_bytes = TOTP::new_with_digest(
secret.as_bytes(),
MacDigest::SHA256
);
// Get an otp with the given counter, time and other custom params
let otp_sha256 = totp_sha256_bytes.get_otp_with_custom(
elapsed_seconds,
30, // A 60-second time step
0, // Start time at unix epoch
6 // 8-digit code
);
println!("The otp from totp_sha256_bytes: {}", otp_sha256);
} ```
The changelog for this crate can be found at CHANGELOG.md
Please file any featre requests or bug reports through the [issue tracker]