Struct totp_rs::TOTP

source · []
pub struct TOTP<T = Vec<u8>> {
    pub algorithm: Algorithm,
    pub digits: usize,
    pub skew: u8,
    pub step: u64,
    pub secret: T,
}
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: Algorithm

SHA-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: usize

The number of digits composing the auth code. Per rfc-4226, this can oscilate between 6 and 8 digits

skew: u8

Number 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: u64

Duration in seconds of a step. The recommended value per rfc-6238 is 30 seconds

secret: T

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

Implementations

Will create a new instance of TOTP with given parameters. See the doc for reference as to how to choose those values

Will sign the given timestamp

Will generate a token according to the provided timestamp in seconds

Will check if token is valid by current time, accounting skew

Will return the base32 representation of the secret, which might be useful when users want to manually add the secret to their authenticator

Will generate a standard URL used to automatically add TOTP auths. Usually used with qr codes

Will return a qrcode to automatically add a TOTP as a base64 string. Needs feature qr to be enabled!

Errors

This will return an error in case the URL gets too long to encode into a QR code

It will also return an error in case it can’t encode the qr into a png. This shouldn’t happen unless either the qrcode library returns malformed data, or the image library doesn’t encode the data correctly

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.