Sample code for 30+ languages & platforms
Rust

Generate TOTP using Base32 Secret (6 Digits, Time-based, 30-second period)

See more Encryption Examples

Generates a 6-digit time-based TOTP code using a base32 secret with a 30-second time period.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// 
let crypt = chilkat::Crypt2::new();

// Base32 secret key:
let secret_key = "oebf ytfl qmzb p4xd 2ztf zyz4 hjrw 3uyo".to_string();

let sb_key = chilkat::StringBuilder::new();
let _ = sb_key.append(&secret_key).is_ok();

// Remove SPACE chars.
let num_removed = sb_key.replace(" ", "");

// Note: A new token is generated every 30 seconds.  You must generate within the same 30 second interval to get the same result.

// Generate 6 digits.
let num_digits = 6;
// 30 second time period.
let time_period = 30;
println!("Your token is: {}", crypt.totp(&sb_key.get_as_string().unwrap_or_default(), "base32", "0", "", time_period, num_digits, -1, "sha1").unwrap_or_default());