#otp #totp #google-authenticator

google-authenticator

This Rust crate can be used to interact with the Google Authenticator mobile app for 2-factor-authentication

6 releases

0.3.0 Nov 15, 2021
0.2.1 Jul 22, 2020
0.1.9 Mar 5, 2020
0.1.8 Aug 18, 2019
0.1.5 Apr 16, 2018

#170 in Cryptography

Download history 1701/week @ 2022-06-11 1832/week @ 2022-06-18 1751/week @ 2022-06-25 1951/week @ 2022-07-02 1946/week @ 2022-07-09 2193/week @ 2022-07-16 2176/week @ 2022-07-23 2544/week @ 2022-07-30 2652/week @ 2022-08-06 2096/week @ 2022-08-13 2038/week @ 2022-08-20 2289/week @ 2022-08-27 2336/week @ 2022-09-03 2616/week @ 2022-09-10 3586/week @ 2022-09-17 1765/week @ 2022-09-24

10,761 downloads per month
Used in 8 crates (4 directly)

MIT license

25KB
359 lines

GoogleAuthenticator

Build Status Build Status

Introduction

This Rust crate can be used to interact with the Google Authenticator mobile app for 2-factor-authentication.This Rust crates can generate secrets, generate codes, validate codes and present a QR-Code for scanning the secret.It implements TOTP according to RFC6238 More about Google GoogleAuthenticator see:Wiki

Usage

Add this to your Cargo.toml:

[dependencies]
google-authenticator = "0.2.0"

Examples

use google_authenticator::GoogleAuthenticator;

fn main() {
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    let auth = GoogleAuthenticator::new();
    // let secret = auth.create_secret(32);
    let code = auth.get_code(&secret, 0).unwrap();

    assert!(auth.verify_code(&secret, &code, 1, 0).unwrap());
}
#[macro_use]
extern crate google_authenticator;
use google_authenticator::GA_AUTH;

fn main() {
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    if let Ok(code) = get_code!(&secret) {
        println!("{}", verify_code!(&secret, &code, 1, 0));
    }
}

Get the secret QR code

Get Google Charts Url to make QR Code

use google_authenticator::{GoogleAuthenticator, ErrorCorrectionLevel};

fn main() {
    let auth = GoogleAuthenticator::new();
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    println!(
        "{}",
        auth.qr_code_url(secret, "qr_code", "name", 200, 200, ErrorCorrectionLevel::High)
    );
}
#[macro_use]
extern crate google_authenticator;
use google_authenticator::GA_AUTH;

fn main() {
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    println!("{}", qr_code_url!(&secret, "qr_code", "name"));
}

Get QR code image in svg format

Change Cargo.toml to

[dependencies.google-authenticator]
version = "0.1.9"
features = ["with-qrcode"]
use google_authenticator::{GoogleAuthenticator, ErrorCorrectionLevel};

fn main() {
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    let auth = GoogleAuthenticator::new();

    println!(
        "{}",
        auth.qr_code(secret, "qr_code", "name", 200, 200, ErrorCorrectionLevel::High)
            .unwrap()
    );
}
#[macro_use]
extern crate google_authenticator;
use google_authenticator::GA_AUTH;

fn main() {
    let secret = "I3VFM3JKMNDJCDH5BMBEEQAW6KJ6NOE3";
    if let Ok(url) = qr_code!(&secret, "qr_code", "name") {
        println!("{}", url);
    }
}

Contributors

Thanks to: JHZheng Conbas

FAQ

You can post a new issue for help.

Dependencies

~370–730KB
~14K SLoC