totp-rs/README.md

71 lines
2.1 KiB
Markdown
Raw Normal View History

2020-04-11 15:24:50 -04:00
# totp-rs
2022-01-13 12:33:59 -05:00
![Build Status](https://github.com/constantoine/totp-rs/workflows/Rust/badge.svg) [![docs](https://docs.rs/totp-rs/badge.svg)](https://docs.rs/totp-rs) [![](https://img.shields.io/crates/v/totp-rs.svg)](https://crates.io/crates/totp-rs) [![codecov](https://codecov.io/gh/constantoine/totp-rs/branch/master/graph/badge.svg?token=Q50RAIFVWZ)](https://codecov.io/gh/constantoine/totp-rs) [![cargo-audit](https://github.com/constantoine/totp-rs/actions/workflows/security.yml/badge.svg)](https://github.com/constantoine/totp-rs/actions/workflows/security.yml)
2020-04-11 15:24:50 -04:00
2020-08-09 02:36:06 -04:00
This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as lightweight as possible to ensure small binaries and short compilation time
2020-04-11 15:24:50 -04:00
## Features
---
### qr
With optional feature "qr", you can use it to generate a base64 png qrcode
### serde_support
With optional feature "serde_support", library-defined types will be Deserialize-able and Serialize-able
2020-04-11 15:24:50 -04:00
## How to use
---
2020-04-11 19:08:38 -04:00
Add it to your `Cargo.toml`:
2020-04-11 15:24:50 -04:00
```toml
2020-04-13 10:59:30 -04:00
[dependencies]
2022-01-13 10:17:19 -05:00
totp-rs = "~0.7"
2020-04-11 15:24:50 -04:00
```
2020-04-13 16:57:06 -04:00
You can then do something like:
```Rust
use std::time::SystemTime;
2020-06-21 09:57:16 -04:00
use totp_rs::{Algorithm, TOTP};
2020-04-13 16:57:06 -04:00
2020-06-21 09:57:16 -04:00
let totp = TOTP::new(
Algorithm::SHA1,
6,
1,
30,
"supersecret",
2020-06-21 09:57:16 -04:00
);
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH).unwrap()
.as_secs();
let url = totp.get_url("user@example.com", "my-org.com");
2020-04-13 16:57:06 -04:00
println!("{}", url);
let token = totp.generate(time);
println!("{}", token);
```
2020-06-21 09:57:16 -04:00
### With qrcode generation
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
2022-01-13 10:17:19 -05:00
version = "~0.7"
2020-06-21 09:57:16 -04:00
features = ["qr"]
```
You can then do something like:
```Rust
use totp_rs::{Algorithm, TOTP};
let totp = TOTP::new(
Algorithm::SHA1,
6,
1,
30,
"supersecret",
2020-06-21 09:57:16 -04:00
);
let code = totp.get_qr("user@example.com", "my-org.com")?;
2020-06-21 09:57:16 -04:00
println!("{}", code);
```
### With serde support
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
2022-01-13 10:17:19 -05:00
version = "~0.7"
features = ["serde_support"]
```