totp-rs
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 lightweight as possible to ensure small binaries and short compilation time.
It now supports parsing otpauth URLs into a totp object, with sane default values.
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.
Features
qr
With optional feature "qr", you can use it to generate a base64 png qrcode. This will enable feature otpauth.
otpauth
With optional feature "otpauth", support parsing the TOTP parameters from an otpauth URL, and generating an otpauth URL. It adds 2 fields to Totp.
serde
With optional feature "serde", library-defined types Totp, Algorithm and Secret will be Deserialize-able and Serialize-able.
gen_secret
With optional feature "gen_secret", a secret will be generated for you to store in database.
zeroize
Securely zero secret information when the TOTP struct is dropped.
steam
Add support for Steam TOTP tokens.
migration
Enabled by default. Provides deprecated aliases and shims for the 5.7.x API so existing code keeps compiling while you port it to the 6.0 Builder API (see MIGRATION.md). Disable it with default-features = false once you have finished migrating to turn any remaining 5.x calls into hard errors.
std
Enabled by default, and enables alloc. Gives access to the methods that read the system clock: generate_current, check_current, ttl and next_step_current. Without it, pass the current Unix timestamp yourself to generate, check and next_step.
alloc
Enabled by std, otpauth, qr and migration. Required by everything that allocates: the base32 conversions (Secret::try_from_base32 and Secret::to_base32), the String conversions of Algorithm, and secrets of any length other than 20 bytes.
Examples
Summary
- Understanding Secret
- Generate a token
- Enable qrcode generation
- Enable otpauth url support
- Enable gen_secret support
- With RFC-6238 compliant default
- New TOTP from steam secret
- Using it without
std
Understanding Secret
Secret is an opaque type holding the secret's raw bytes. You can build one
either from raw bytes or from a base32-encoded string; both yield the same value.
from
Is equivalent to:
try_from_base32.unwrap
Generate a token
Add it to your Cargo.toml:
[]
= "^6.0"
You can then do something like:
use ;
Which is equivalent to:
use ;
generate_current returns a Token. It implements Display, so it prints
directly; call .to_string() if you need an owned String.
With qrcode generation
Add it to your Cargo.toml:
[]
= "^6.0"
= ["qr"]
You can then do something like:
use ;
With otpauth url support
Add it to your Cargo.toml:
[]
= "^6.0"
= ["otpauth"]
You can then do something like:
use Totp;
With gen_secret
Add it to your Cargo.toml:
[]
= "^6.0"
= ["gen_secret"]
With gen_secret, Builder::new() already holds a freshly generated secret, so you can build a Totp without supplying one:
use ;
Which is equivalent to setting the secret explicitly:
use ;
With RFC-6238 compliant default
Builder::new() already starts from RFC-6238 compliant defaults (SHA1, 6 digits,
skew of 1, 30 second step), and build() enforces that compliance. So the only
mandatory step is providing a secret:
use ;
is equivalent to:
use ;
You can override each property individually:
use ;
You can ignore compliance checks by using Builder::build_noncompliant.
Note that the checks are here for a reason, and unless you know what you're doing, you should really not have a lot of usecases for this one.
use ;
With gen_secret feature, you can go even further and have all values by default and a secure secret.
Note: With otpauth feature, the issuer defaults to None and the account name to "". Be sure to set them with Builder::with_issuer and Builder::with_account_name before generating an URL/QRCode.
New TOTP from steam secret
Add it to your Cargo.toml:
[]
= "^6.0"
= ["steam"]
You can then do something like:
use ;
Using it without std
The crate is #![no_std] whenever the std feature is off, and needs no allocator at all when alloc is off too. serde, steam, gen_secret and zeroize all work in that configuration. otpauth, qr and migration do not, as they enable alloc.
[]
= "^6.0"
= false
# Anything except otpauth, qr and migration.
= ["steam"]
Without alloc, a secret is exactly the 160 bits rfc-4226 recommends, built with Secret::new_stack:
use ;
With gen_secret, Secret::generate also works without an allocator. See examples/no_alloc.rs for a full bare-metal program; CI builds it for wasm32v1-none.