Crates.io | anyotp |
lib.rs | anyotp |
version | |
source | src |
created_at | 2023-11-19 07:19:46.393218+00 |
updated_at | 2025-01-14 00:59:35.165782+00 |
description | RFC-complaint one-time password algorithms written in Rust |
homepage | https://github.com/nashaofu/ruotp |
repository | https://github.com/nashaofu/ruotp.git |
max_upload_size | |
id | 1040994 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
RFC-complaint one-time password algorithms written in Rust
use ruotp::{HOTP, TOTP};
use std::{
thread,
time::{Duration, SystemTime, UNIX_EPOCH},
};
fn generate_hotp_token(secret: &str, timestamp: u64) -> String {
let hotp = HOTP::from_base32(secret).unwrap();
let hotp_key_uri = hotp.to_key_uri("root", Some("RuOTP"), 1).unwrap();
println!("hotp_key_uri {}", hotp_key_uri);
hotp.generate_token(timestamp / 30).unwrap()
}
fn generate_totp_token(secret: &str, timestamp: u64) -> String {
let totp = TOTP::from_base32(secret).unwrap();
let totp_key_uri = totp.to_key_uri("root", Some("RuOTP")).unwrap();
println!("totp_key_uri {}", totp_key_uri);
totp.generate_token(timestamp).unwrap()
}
fn main() {
let secret = "OA6W5CY6EFGDC5I6";
let now = SystemTime::now();
thread::sleep(Duration::from_millis(100));
let timestamp = now.duration_since(UNIX_EPOCH).unwrap().as_secs();
let hotp_token = generate_hotp_token(secret, timestamp);
let totp_token = generate_totp_token(secret, timestamp);
println!("hotp_token {:?}", hotp_token);
println!("totp_token {:?}", totp_token);
}