pub struct TOTP { /* private fields */ }Expand description
Time-based One-Time Password (TOTP) generator.
Implements RFC 6238.
Implementations§
Source§impl TOTP
impl TOTP
Sourcepub fn builder() -> TOTPBuilder
pub fn builder() -> TOTPBuilder
Returns a new TOTP builder
Sourcepub fn new(
secret: Vec<u8>,
algorithm: Algorithm,
digits: u32,
time_step: u64,
) -> Self
pub fn new( secret: Vec<u8>, algorithm: Algorithm, digits: u32, time_step: u64, ) -> Self
Creates a new TOTP instance with the given parameters
Sourcepub fn generate_at(&self, timestamp: u64) -> u32
pub fn generate_at(&self, timestamp: u64) -> u32
Generates the TOTP value for a specific timestamp
Sourcepub fn generate_current(&self) -> Result<u32, Error>
pub fn generate_current(&self) -> Result<u32, Error>
Generates the TOTP value for the current system time
§Errors
Returns error if system time is before Unix epoch.
Sourcepub fn generate_formatted_at(&self, timestamp: u64) -> String
pub fn generate_formatted_at(&self, timestamp: u64) -> String
Generates the TOTP value as a formatted string with leading zeros for a specific timestamp
Sourcepub fn generate_current_formatted(&self) -> Result<String, Error>
pub fn generate_current_formatted(&self) -> Result<String, Error>
Generates the formatted TOTP value (with leading zeros) for the current system time
§Errors
Returns error if system time is before Unix epoch.
Sourcepub fn verify_at(&self, code: u32, timestamp: u64, window: u32) -> bool
pub fn verify_at(&self, code: u32, timestamp: u64, window: u32) -> bool
Verifies a TOTP code at a specific timestamp with a given look-back/forward window.
The window is the number of steps to check before and after the current step. For example, a window of 1 means it will check the current, previous, and next step.