#totp #hotp #2fa #otp

google-authenticator

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

7 unstable releases

0.4.2 May 6, 2023
0.4.0 Mar 9, 2023
0.3.0 Nov 15, 2021
0.2.1 Jul 22, 2020
0.1.5 Apr 16, 2018

#40 in Authentication

Download history 1111/week @ 2023-12-17 383/week @ 2023-12-24 509/week @ 2023-12-31 929/week @ 2024-01-07 934/week @ 2024-01-14 999/week @ 2024-01-21 850/week @ 2024-01-28 789/week @ 2024-02-04 906/week @ 2024-02-11 1072/week @ 2024-02-18 1017/week @ 2024-02-25 1210/week @ 2024-03-03 951/week @ 2024-03-10 1147/week @ 2024-03-17 1086/week @ 2024-03-24 1611/week @ 2024-03-31

4,819 downloads per month
Used in 12 crates (7 directly)

MIT license

31KB
453 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.4"

C/C++ lib

You can find the header file from src/authenticator.h, and then build the lib for your target.

How to make header file and build lib, you can refer to the following case.

Tools you may need: rust-lipo cbingen

## gen c/c++ header file
cbindgen ./ -l c --output src/authenticator.h

## clone registry
git clone https://github.com/hanskorg/google-authenticator-rust.git && cd google-authenticator-rust

## change Cargo.toml
crate-type = ["staticlib","cdylib"]
required-features = ["with-qrcode","clib"]

## build for MacOS and IOS
cargo lipo --features with-qrcode --targets aarch64-apple-darwin  x86_64-apple-darwin aarch64-apple-ios

## build for linux musl
cargo build --all-features  --lib --release --target x86_64-unknown-linux-musl

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.4"
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

~10–19MB
~42K SLoC