Crates.io | totp_rfc6238 |
lib.rs | totp_rfc6238 |
version | 0.6.1 |
source | src |
created_at | 2021-04-20 12:52:47.273171 |
updated_at | 2024-08-15 04:37:55.548997 |
description | library for generating Time-based One-time Password (TOTP) codes/tokens defined in RFC 6238 |
homepage | |
repository | https://github.com/KaneGreen/totp_rfc6238 |
max_upload_size | |
id | 387062 |
size | 69,319 |
A rust crate for generating TOTP codes (tokens) defined in RFC 6238.
otpauth://totp/
) (the oathuri
feature gate).key
from base32-encoded string (the oathuri
feature gate).[dependencies]
totp_rfc6238 = "0.6"
[dependencies]
totp_rfc6238 = { version = "0.6", default-features = false, features = ["ring"] }
This implementation does NOT consider the time earlier than the
Unix epoch (1970-01-01T00:00:00Z
).
use totp_rfc6238::{HashAlgorithm, TotpGenerator};
fn main() {
// Create a standard TOTP code generator: 6-digit, updating every
// 30 seconds, starting at "Jan 01 1970 00:00:00 UTC", using HMAC-SHA1.
let mut totp_generator = TotpGenerator::new().build();
// Assuming you have read the key from somewhere secure
let key = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890+/";
let output1 = totp_generator.get_code(key);
println!("Your TOTP code for current time is: {}", output1);
let output2 = totp_generator.get_next_update_time().unwrap();
println!("Next update will be at the unix timestamp of {}", output2);
let output3 = totp_generator.get_code_window(key, -4..=4).unwrap();
println!("Codes for 2 minutes earlier or later are:");
for i in output3 {
println!(" {}", i);
}
// You can also create a non-standard TOTP code generator: 8-digit,
// updating every 90 seconds, starting at "Jan 01 1970 00:16:42 UTC",
// using HMAC-SHA512.
let mut another_totp_generator = TotpGenerator::new()
.set_digit(8).unwrap()
.set_step(90).unwrap()
.set_t0(16 * 60 + 42)
.set_hash_algorithm(HashAlgorithm::SHA512)
.build();
let output4 = another_totp_generator.get_code(key);
println!("Your non-standard TOTP code for current time is: {}", output4);
}
See here.
The version number lower than 1.0.0
should be regarded as an unstable version
of the API. Therefore, some version updates may contain incompatible API
changes. Please refer to the following when changing the dependent version.
oath_uri::TotpUri
and oath_uri::KeyInfo
implemented the Debug
trait in version 0.5.2, but this was removed in 0.5.3.
(only affects the oathuri
feature)oathuri
feature)oathuri
feature)oathuri
feature)The codes of this crate has not been audited.
This tool is primarily distributed under the terms of both the MIT license
and the Apache License (Version 2.0), with portions covered by various
BSD-like licenses.
See LICENSE-APACHE, LICENSE-MIT for details.