totp-rs is #![no_std]. If you disable the std feature, you lose access to methods that rely on the system clock (like generate_current, check_current, ttl, and next_step_current).
In a no_std environment, you must provide the Unix timestamp manually to the generate, check, and next_step methods.
Configuration for no_std
To minimize dependencies, disable default-features. Note that otpauth, qr, and migration require alloc and cannot be used in a no_alloc environment.
[dependencies.totp-rs]
version = "^6.0"
default-features = false
features = ["steam"]
Example: no_std with manual timestamp
use totp_rs::{Builder, Secret, Totp};
fn generate_manual(unix_time: u64) -> String {
let totp: Totp = Builder::new()
.with_secret(Secret::new_stack(*b"12345678901234567890"))
.build()
.unwrap();
// Use generate(timestamp) instead of generate_current()
totp.generate(unix_time).to_string()
}
use totp_rs::{Builder, Secret, Totp};
fn token_at(unix_time: u64) -> impl core::fmt::Display {
let totp: Totp = Builder::new()
.with_secret(Secret::new_stack(*b"12345678901234567890"))
.build()
.unwrap();
totp.generate(unix_time)
}