add `ttl` to TOTP
This commit is contained in:
parent
181d17edcc
commit
40196c5ed9
|
@ -0,0 +1,23 @@
|
||||||
|
use totp_rs::{Algorithm, TOTP};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let totp = TOTP::new(
|
||||||
|
Algorithm::SHA1,
|
||||||
|
6,
|
||||||
|
1,
|
||||||
|
30,
|
||||||
|
"my-secret".to_string(),
|
||||||
|
None,
|
||||||
|
"account".to_string(),
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
println!(
|
||||||
|
"code {}\t ttl {}\t valid until: {}",
|
||||||
|
totp.generate_current().unwrap(),
|
||||||
|
totp.ttl().unwrap(),
|
||||||
|
totp.next_step_current().unwrap()
|
||||||
|
);
|
||||||
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||||
|
}
|
||||||
|
}
|
|
@ -233,6 +233,12 @@ impl<T: AsRef<[u8]>> TOTP<T> {
|
||||||
Ok(self.next_step(t))
|
Ok(self.next_step(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Give the ttl (in seconds) of the current token
|
||||||
|
pub fn ttl(&self) -> Result<u64, SystemTimeError> {
|
||||||
|
let t = system_time()?;
|
||||||
|
Ok(self.step - (t % self.step))
|
||||||
|
}
|
||||||
|
|
||||||
/// Generate a token from the current system time
|
/// Generate a token from the current system time
|
||||||
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
|
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
|
||||||
let t = system_time()?;
|
let t = system_time()?;
|
||||||
|
|
Loading…
Reference in New Issue