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).``` use totprfc6238::{HashAlgorithm, TotpGenerator}; let key = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890+/"; // Create a non-standard TOTP code generator: 8-digit, updating every 60 // seconds, starting at "Jan 01 1970 00:16:40 UTC", using HMAC-SHA512. let mut totpgenerator = TotpGenerator::new() .setdigit(8).unwrap() .setstep(60).unwrap() .sett0(1000) .sethash_algorithm(HashAlgorithm::SHA512) .build();
let output1 = totpgenerator.getcode(key); println!("Your TOTP code for current time is: {}", output1);
let output2 = totpgenerator.getnextupdatetime().unwrap(); println!("Next update will be at the unix timestamp of {}", output2);
let output3 = totpgenerator.getcode_window(key, -5..=5).unwrap(); println!("Codes for 5 minutes earlier or later are:"); for i in output3 { println!(" {}", i); } ```
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.