1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::hotp::HOTP;
use std::time::SystemTime;

fn create_counter(period: u64) -> u64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs()
        / period
}

pub struct TOTP {
    pub hotp: HOTP,
    pub digits: u32,
    pub period: u64,
}

impl TOTP {
    pub fn new(secret: String, digits: u32, period: u64) -> TOTP {
        TOTP {
            hotp: HOTP::new(secret, 0),
            digits,
            period,
        }
    }

    pub fn make(&mut self) -> String {
        self.hotp.counter = create_counter(self.period);
        self.hotp.make(self.digits)
    }

    pub fn check(&mut self, otp: &str, window: u64) -> bool {
        self.hotp.counter = create_counter(self.period);
        self.hotp.check(otp, window)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let encoded_secret = "MU2TSNRZG5TGKMBYGAZDCMJTMM3GIMJVMZRTINDFGI3WGZRVMQ4Q"; // The secret key is a base32 encoded string

        let secret_vec =
            base32::decode(base32::Alphabet::RFC4648 { padding: false }, encoded_secret).unwrap();
        let secret = String::from_utf8(secret_vec).unwrap();

        let period = 30; // 30 seconds
        let digits = 6; // 6 digits

        let mut totp = crate::totp::TOTP::new(secret, digits, period);

        assert_eq!(totp.make().len(), digits as usize);
    }

    #[test]
    fn token_is_the_same_when_called_twice_quickly() {
        let encoded_secret = "MU2TSNRZG5TGKMBYGAZDCMJTMM3GIMJVMZRTINDFGI3WGZRVMQ4Q"; // The secret key is a base32 encoded string

        let secret_vec =
            base32::decode(base32::Alphabet::RFC4648 { padding: false }, encoded_secret).unwrap();
        let secret = String::from_utf8(secret_vec).unwrap();

        let period = 30; // 30 seconds
        let digits = 6; // 6 digits

        let mut totp1 = crate::totp::TOTP::new(secret.clone(), digits, period);
        let mut totp2 = crate::totp::TOTP::new(secret, digits, period);
        assert_eq!(totp1.make(), totp2.make());
    }
}