Crates.io | r2fa |
lib.rs | r2fa |
version | 0.5.0 |
source | src |
created_at | 2015-07-26 15:20:53.542929 |
updated_at | 2015-12-16 00:01:56.610782 |
description | Rust Two-Factor Authentication (R2FA) is a collection of tools for two-factor authentication. |
homepage | |
repository | https://github.com/breard-r/r2fa |
max_upload_size | |
id | 2684 |
size | 58,353 |
Rust Two-Factor Authentication (R2FA) is a collection of tools for two-factor authentication.
HOTP - HMAC-based One-time Password Algorithm (RFC 4226)
TOTP - Time-based One-time Password Algorithm (RFC 6238)
U2F - Universal 2nd Factor (FIDO Alliance)
You can find R2FA on crates.io and include it in your Cargo.toml
:
r2fa = "*"
In order to build R2FA, you will need both the rust compiler and cargo.
$ git clone https://github.com/breard-r/r2fa.git
$ cd r2fa
$ make
$ make install prefix=/usr
More examples are available in the documentation.
extern crate r2fa;
use r2fa::otp::TOTPBuilder;
let key = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ".to_string();
let code = TOTPBuilder::new()
.base32_key(&key)
.finalize()
.unwrap()
.generate();
assert_eq!(code.len(), 6);
#include <stdio.h>
#include <r2fa.h>
int main(void) {
struct r2fa_totp_cfg cfg;
char code[7], key[] = "12345678901234567890";
if (r2fa_totp_init(&cfg) != R2FA_OTP_SUCCESS) {
return 1;
}
cfg.key = key;
cfg.key_len = sizeof(key);
if (r2fa_totp_generate(&cfg, code) != R2FA_OTP_SUCCESS) {
return 2;
}
printf("%s\n", code);
return 0;
}
$ cc -o totp totp.c -lr2fa
$ ./totp
848085
from ctypes.util import find_library
from struct import Struct
from ctypes import *
class TOTPcfg(Structure):
_fields_ = [
('key', c_char_p),
('key_len', c_size_t),
('timestamp', c_longlong),
('period', c_uint),
('initial_time', c_ulonglong),
('output_len', c_size_t),
('output_base', c_char_p),
('output_base_len', c_size_t),
('hash_function', c_int),
]
def get_totp():
key = b'12345678901234567890'
lib_path = find_library('r2fa') or 'target/release/libr2fa.so'
lib = cdll.LoadLibrary(lib_path)
cfg = TOTPcfg()
if lib.r2fa_totp_init(byref(cfg)) != 0:
return
cfg.key_len = len(key)
cfg.key = c_char_p(key)
code = create_string_buffer(b'\000' * cfg.output_len)
if lib.r2fa_totp_generate(byref(cfg), code) != 0:
return
return str(code.value, encoding="utf-8")
if __name__ == '__main__':
code = get_totp()
print('{}'.format(code))