Edit

thodg/totp-rs/examples/secret.rs

Branch :

  • Show log

    Commit

  • Author : Steven Salaun
    Date : 2022-08-08 20:19:04
    Hash : 3d61027d
    Message : Change Secret: Plain to Raw & Base32 to Encoded

  • examples/secret.rs
  • use totp_rs::{Secret, TOTP, Algorithm};
    
    fn main () {
        // create TOTP from base32 secret
        let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
        let totp_b32 = TOTP::new(
            Algorithm::SHA1,
            6,
            1,
            30,
            secret_b32.to_bytes().unwrap(),
            None,
            "account".to_string(),
        ).unwrap();
    
        println!("base32 {} ; raw {}", secret_b32, secret_b32.to_raw().unwrap());
        println!("code from base32:\t{}", totp_b32.generate_current().unwrap());
    
        // create TOTP from raw binary value
        let secret = [
            0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
            0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
        ];
        let secret_raw = Secret::Raw(secret.to_vec());
        let totp_raw = TOTP::new(
            Algorithm::SHA1,
            6,
            1,
            30,
            secret_raw.to_bytes().unwrap(),
            None,
            "account".to_string(),
        ).unwrap();
    
        println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
        println!("code from raw secret:\t{}", totp_raw.generate_current().unwrap());
    }