DEV Community

Opium ver. K
Opium ver. K

Posted on

Rust Realtime OTP Algorithm

OTP illustration

Time-based One-Time Password (TOTP) Algorithm Implementation in Rust

This Rust program provides an implementation of the Time-based One-Time Password (TOTP) algorithm, which is used to generate one-time passwords for multi-factor authentication systems. The algorithm is based on a shared secret and the current time, and produces a 6-digit numerical code that changes every 30 seconds.

Implementation Details

The core function of the algorithm is generate_otp(secret: &str), which takes a shared secret as input and returns a 6-digit numerical code that changes every 30 seconds. The function uses the following steps to generate the OTP:

  1. Get the current time in seconds since the Unix epoch using the SystemTime and UNIX_EPOCH structs.
  2. Divide the current time by 30 to get the current time window.
  3. Generate a random number between 0 and 999999 using the thread_rng() function from the rand crate.
  4. XOR the current time window with the random number to produce an intermediate value.
  5. Take the intermediate value mod 1000000 to produce a 6-digit OTP.

Example Usage

The main() function of the program demonstrates how this OTP algorithm can be used in practice, by calling the generate_otp function in a loop and printing the resulting OTP to the console every 30 seconds. This simulates the real-time generation of OTPs that would typically be used in multi-factor authentication systems.

use rand::{Rng, thread_rng};
use std::time::{SystemTime, UNIX_EPOCH};
use std::thread;
use std::time;

fn generate_otp(secret: &str) -> u32 {
    let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() / 30;
    let mut rng = thread_rng();
    let random_number: u32 = rng.gen_range(0..1000000);
    let otp = (now as u32 ^ random_number) % 1000000;


fn main() {
    let secret = "xxx_Your_Secret_Here_xxx";
    loop {
        let otp = generate_otp(secret);
        println!("Your OTP is: {}", otp);

Enter fullscreen mode Exit fullscreen mode

Security Considerations

Note that the security of the TOTP algorithm depends on the secrecy of the shared secret, which should be kept confidential and not shared with unauthorized users. Additionally, the program should be used in conjunction with appropriate security measures, such as SSL/TLS encryption and strong password policies, to ensure the security of user accounts and sensitive data.


If you'd like to get a tutorial that includes SSL/TLS please interact or contact me via twitter @SensoryKopi

Top comments (1)

johnram98468527 profile image

Hello. It was interesting to read your article. Because nowadays this security system is really used in many places, as it is quite resistant to cryptographic attacks, unlike other methods. So if you are looking for a reliable and powerful rfc generator based on algorithm based one time password (TOTP), I recommend you this site. I have already been convinced of their safety and reliability in practice.