pub struct TOTP {
pub algorithm: Algorithm,
pub digits: usize,
pub skew: u8,
pub step: u64,
pub secret: Vec<u8>,
}Expand description
TOTP holds informations as to how to generate an auth code and validate it. Its secret field is sensitive data, treat it accordingly
Fields§
§algorithm: AlgorithmSHA-1 is the most widespread algorithm used, and for totp pursposes, SHA-1 hash collisions are not a problem as HMAC-SHA-1 is not impacted. It’s also the main one cited in rfc-6238 even though the reference implementation permits the use of SHA-1, SHA-256 and SHA-512. Not all clients support other algorithms then SHA-1
digits: usizeThe number of digits composing the auth code. Per rfc-4226, this can oscilate between 6 and 8 digits
skew: u8Number of steps allowed as network delay. 1 would mean one step before current step and one step after are valids. The recommended value per rfc-6238 is 1. Anything more is sketchy, and anyone recommending more is, by definition, ugly and stupid
step: u64Duration in seconds of a step. The recommended value per rfc-6238 is 30 seconds
secret: Vec<u8>As per rfc-4226 the secret should come from a strong source, most likely a CSPRNG. It should be at least 128 bits, but 160 are recommended
non-encoded value
Implementations§
Source§impl TOTP
impl TOTP
Sourcepub fn new(
algorithm: Algorithm,
digits: usize,
skew: u8,
step: u64,
secret: Vec<u8>,
) -> Result<TOTP, TotpUrlError>
pub fn new( algorithm: Algorithm, digits: usize, skew: u8, step: u64, secret: Vec<u8>, ) -> Result<TOTP, TotpUrlError>
Will create a new instance of TOTP with given parameters. See the doc for reference as to how to choose those values
§Description
secret: expect a non-encoded value, to pass in base32 string useSecret::Encoded(String)digits: MUST be between 6 & 8secret: Must have bitsize of at least 128
§Example
use totp_rs::{Secret, TOTP, Algorithm};
let secret = Secret::Encoded("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG".to_string());
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, secret.to_bytes().unwrap()).unwrap();§Errors
Will return an error if the digit or secret size is invalid
Sourcepub fn new_unchecked(
algorithm: Algorithm,
digits: usize,
skew: u8,
step: u64,
secret: Vec<u8>,
) -> TOTP
pub fn new_unchecked( algorithm: Algorithm, digits: usize, skew: u8, step: u64, secret: Vec<u8>, ) -> TOTP
Will create a new instance of TOTP with given parameters. See the doc for reference as to how to choose those values. This is unchecked and does not check the digits and secret size
§Description
secret: expect a non-encoded value, to pass in base32 string useSecret::Encoded(String)
§Example
use totp_rs::{Secret, TOTP, Algorithm};
let secret = Secret::Encoded("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG".to_string());
let totp = TOTP::new_unchecked(Algorithm::SHA1, 6, 1, 30, secret.to_bytes().unwrap());Sourcepub fn from_rfc6238(rfc: Rfc6238) -> Result<TOTP, TotpUrlError>
pub fn from_rfc6238(rfc: Rfc6238) -> Result<TOTP, TotpUrlError>
Sourcepub fn generate(&self, time: u64) -> String
pub fn generate(&self, time: u64) -> String
Will generate a token given the provided timestamp in seconds
Sourcepub fn next_step(&self, time: u64) -> u64
pub fn next_step(&self, time: u64) -> u64
Returns the timestamp of the first second for the next step given the provided timestamp in seconds
Sourcepub fn next_step_current(&self) -> Result<u64, SystemTimeError>
pub fn next_step_current(&self) -> Result<u64, SystemTimeError>
Returns the timestamp of the first second of the next step According to system time
Sourcepub fn ttl(&self) -> Result<u64, SystemTimeError>
pub fn ttl(&self) -> Result<u64, SystemTimeError>
Give the ttl (in seconds) of the current token
Sourcepub fn generate_current(&self) -> Result<String, SystemTimeError>
pub fn generate_current(&self) -> Result<String, SystemTimeError>
Generate a token from the current system time
Sourcepub fn check(&self, token: &str, time: u64) -> bool
pub fn check(&self, token: &str, time: u64) -> bool
Will check if token is valid given the provided timestamp in seconds, accounting skew
Sourcepub fn check_current(&self, token: &str) -> Result<bool, SystemTimeError>
pub fn check_current(&self, token: &str) -> Result<bool, SystemTimeError>
Will check if token is valid by current system time, accounting skew
Sourcepub fn get_secret_base32(&self) -> String
pub fn get_secret_base32(&self) -> String
Will return the base32 representation of the secret, which might be useful when users want to manually add the secret to their authenticator