Crate easy_totp

Source
Expand description

easy_totp is a crate designed to make it easy to integrate TOTP into your Rust apps.

BEWARE: handle secrets with caution

§Creating a QR code for TOTP setup

use easy_totp::EasyTotp;

let raw_secret = String::from("SUPERSecretSecretSecret");
let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");

let my_qr_code = EasyTotp::create_qr_png(raw_secret, issuer, account_name);

§Saving that QR code to a file

use easy_totp::EasyTotp;
use std::fs;
use std::io::Write;

let raw_secret = String::from("SUPERSecretSecretSecret");
let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");
let filename = "./test_images/qr_code.png";

let my_qr_code = EasyTotp::create_qr_png(raw_secret, issuer, account_name);

match my_qr_code {
    Ok(png_data) => {
        let mut file = fs::File::create(filename).unwrap();
        file.write_all(&png_data).unwrap();
        println!("QR code saved as 'qr_code.png'");
    }
    Err(e) => {
        panic!("Error creating QR code: {:?}", e);
    }
}

§Generating TOTP codes for authentication

use easy_totp::EasyTotp;

let raw_secret = String::from("SUPERSecretSecretSecret");
let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");

let token = EasyTotp::generate_token(raw_secret, issuer, account_name).unwrap();

Structs§

EasyTotp
EasyTotp is a unit-struct to keep track of externally-implemented code.