Skip to main content

Crate totp_rs

Crate totp_rs 

Source
Expand description

This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as low-dependency as possible to ensure small binaries and short compilation time.

Be aware that some authenticator apps will accept the SHA256 and SHA512 algorithms but silently fallback to SHA1 which will make the check() function fail due to mismatched algorithms.

Use the SHA1 algorithm to avoid this problem.

§Examples

use totp_rs::{Algorithm, Builder, Totp};

let secret: Vec<u8> = vec![0; 20]; // You want an actual 20bytes of randomness here.

let totp: Totp = Builder::new().
    with_algorithm(Algorithm::SHA256).
    with_secret(secret).
    with_account_name("constantoine@github.com").
    with_issuer(Some("Github")).
    build().
    unwrap();

let token = totp.generate_current();
println!("{}", token);
use totp_rs::{Builder, Totp};

let totp: Totp = Builder::new().
    build().
    unwrap();

let token = totp.generate_current();
println!("{}", token);

let secret = totp.secret().as_bytes();
use totp_rs::{Algorithm, Builder, Totp};

let secret: Vec<u8> = vec![0; 20]; // You want an actual 20bytes of randomness here.

let totp: Totp = Builder::new().
    with_secret(secret).
    with_account_name("constantoine@github.com").
    with_issuer(Some("Github")).
    build().
    unwrap();

let url = totp.to_url().unwrap();
println!("{}", url);
let code = totp.to_qr_base64().unwrap();
println!("{}", code);

Structs§

Builder
Builder used to build a Totp with sane defaults. Because it contains the sensitive data of the HMAC secret, treat it accordingly.
Secret
Shared secret between client and server to validate token against/generate token from.
Token
Represents a token generated by Totp. This can be thought of as a string with a few notable difference:
Totp
TOTP holds informations as to how to generate an auth code and validate it. Its secret field is sensitive data, treat it accordingly.

Enums§

Algorithm
Algorithm enum holds the three standards algorithms for TOTP as per the reference implementation
SecretParseError
Different ways secret parsing failed.
TotpError
Errors produced when building or parsing a Totp.

Type Aliases§

Rfc6238Deprecatedmigration
Rfc6238ErrorDeprecatedmigration
TOTPDeprecatedmigration
TotpUrlErrorDeprecatedmigration