R2FA

Build Status R2FA on crates.io R2FA on GitHub

Rust Two-Factor Authentication (R2FA) is a collection of tools for two-factor authentication.

Features

Using within a Rust project

You can find R2FA on crates.io and include it in your Cargo.toml:

toml r2fa = "^0.1.0"

Using outside Rust

In order to build R2FA, you will need both the rust compiler and cargo.

ShellSession $ git clone https://github.com/breard-r/r2fa.git $ cd r2fa $ make $ make install prefix=/usr

Quick examples

Rust

More examples are available in the documentation.

```rust extern crate r2fa; use r2fa::otp::TOTPBuilder;

let key = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ".tostring(); let code = TOTPBuilder::new() .base32key(&key) .finalize() .unwrap() .generate(); assert_eq!(code.len(), 6); ```

C

```C

include

include

int main(void) { struct r2fatotpcfg cfg; char code[7], key[] = "12345678901234567890";

if (r2fatotpinit(&cfg) != 0) { return 1; } cfg.key = key; cfg.keylen = sizeof(key); if (r2fatotp_generate(&cfg, code) != 0) { return 2; }

printf("%s\n", code);

return 0; } ```

ShellSession $ cc -o totp totp.c -lr2fa $ ./totp 848085

Python

```Python from ctypes.util import find_library from struct import Struct from ctypes import *

class TOTPcfg(Structure): fields = [ ('key', ccharp), ('keylen', csizet), ('timestamp', clonglong), ('period', cuint), ('initialtime', culonglong), ('outputlen', csizet), ('outputbase', ccharp), ('outputbaselen', csizet), ('hashfunction', c_int), ]

def gettotp(): key = b'12345678901234567890' libpath = findlibrary('r2fa') or 'target/release/libr2fa.so' lib = cdll.LoadLibrary(libpath) cfg = TOTPcfg() if lib.r2fatotpinit(byref(cfg)) != 0: return cfg.keylen = len(key) cfg.key = ccharp(key) code = createstringbuffer(b'\000' * cfg.outputlen) if lib.r2fatotpgenerate(byref(cfg), code) != 0: return return str(code.value, encoding="utf-8")

if name == 'main': code = get_totp() print('{}'.format(code)) ```