Additional feature!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
diff --git a/Cargo.toml b/Cargo.toml
index cdbd1a1..763c1dc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,16 +1,21 @@
[package]
name = "totp-rs"
-version = "0.2.6"
+version = "0.3.0"
authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
edition = "2018"
readme = "README.md"
license = "MIT"
-description = "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."
+description = "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. With additional feature \"qr\", you can use it to generate a base64 png qrcode."
repository = "https://github.com/constantoine/totp-rs"
homepage = "https://github.com/constantoine/totp-rs"
keywords = ["authentification", "2fa", "totp", "hmac"]
categories = ["authentication", "web-programming"]
+
+[features]
+default = []
+qr = ["qrcode", "image", "base64"]
+
[dependencies]
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.9.0"
@@ -18,6 +23,6 @@ sha-1 = "0.9.0"
hmac = "0.8.0"
byteorder = ">= 1.3"
base32 = ">= 0.4"
-qrcode = ">= 0.12"
-image = ">= 0.23"
-base64 = ">= 0.12"
+qrcode = { version = ">= 0.12", optional = true }
+image = { version = ">= 0.23", optional = true}
+base64 = { version = ">= 0.12", optional = true }
diff --git a/README.md b/README.md
index 425f459..a837f0a 100644
--- a/README.md
+++ b/README.md
@@ -1,24 +1,56 @@
# totp-rs
-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!
+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! With additional feature "qr", you can use it to generate a base64 png qrcode.
## How to use
Add it to your `Cargo.toml`:
```toml
[dependencies]
-totp-rs = "~0.2"
+totp-rs = "~0.3"
```
You can then do something like:
```Rust
-use totp_rs::{TOTP, Algorithm};
use std::time::SystemTime;
+use totp_rs::{Algorithm, TOTP};
let username = "example".to_string();
-let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "supersecret".to_string().into_bytes());
-let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
+let totp = TOTP::new(
+ Algorithm::SHA1,
+ 6,
+ 1,
+ 30,
+ "supersecret".to_string().into_bytes(),
+);
+let time = SystemTime::now()
+ .duration_since(SystemTime::UNIX_EPOCH).unwrap()
+ .as_secs();
let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
println!("{}", url);
let token = totp.generate(time);
println!("{}", token);
```
+
+### With qrcode generation
+
+Add it to your `Cargo.toml`:
+```toml
+[dependencies.totp-rs]
+version = "~0.3"
+features = ["qr"]
+```
+You can then do something like:
+```Rust
+use totp_rs::{Algorithm, TOTP};
+
+let username = "example".to_string();
+let totp = TOTP::new(
+ Algorithm::SHA1,
+ 6,
+ 1,
+ 30,
+ "supersecret".to_string().into_bytes(),
+);
+let code = totp.get_qr(format!("account:{}", username), "my-org.com".to_string())?;
+println!("{}", code);
+```
diff --git a/src/lib.rs b/src/lib.rs
index 2a27f02..100cd4d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,30 +1,52 @@
-//! 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!
+//! 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! With additional feature "qr", you can use it to generate a base64 png qrcode.
//!
//! # Examples
//!
//! ```
-//! use totp_rs::{TOTP, Algorithm};
//! use std::time::SystemTime;
-//!
+//! use totp_rs::{Algorithm, TOTP};
+//!
//! let username = "example".to_string();
-//! let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "supersecret".to_string().into_bytes());
-//! let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
+//! let totp = TOTP::new(
+//! Algorithm::SHA1,
+//! 6,
+//! 1,
+//! 30,
+//! "supersecret".to_string().into_bytes(),
+//! );
+//! let time = SystemTime::now()
+//! .duration_since(SystemTime::UNIX_EPOCH).unwrap()
+//! .as_secs();
//! let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
//! println!("{}", url);
//! let token = totp.generate(time);
//! println!("{}", token);
//! ```
+//!
+//! ```
+//! use totp_rs::{Algorithm, TOTP};
+//!
+//! let username = "example".to_string();
+//! let totp = TOTP::new(
+//! Algorithm::SHA1,
+//! 6,
+//! 1,
+//! 30,
+//! "supersecret".to_string().into_bytes(),
+//! );
+//! let code = totp.get_qr(format!("account:{}", username), "my-org.com".to_string())?;
+//! println!("{}", code);
+//! ```
use serde::{Deserialize, Serialize};
use base32;
-use base64;
use byteorder::{BigEndian, ReadBytesExt};
use std::io::Cursor;
-use image::Luma;
-use qrcode::QrCode;
+#[cfg(feature = "qr")]
+use {base64, image::Luma, qrcode::QrCode};
use hmac::{Hmac, Mac, NewMac};
use sha1::Sha1;
@@ -135,13 +157,14 @@ impl TOTP {
)
}
- /// Will return a qrcode to automatically add a TOTP as a base64 string
+ /// Will return a qrcode to automatically add a TOTP as a base64 string. Needs feature "qr" to be set. It is by default
///
/// # Errors
///
/// This will return an error in case the URL gets too long to encode into a QR code
///
/// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly
+ #[cfg(feature = "qr")]
pub fn get_qr(
&self,
label: String,
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..7e99d3c
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,20 @@
+use std::time::SystemTime;
+use totp_rs::{Algorithm, TOTP};
+
+fn main() {
+ let username = "example".to_string();
+ let totp = TOTP::new(
+ Algorithm::SHA1,
+ 6,
+ 1,
+ 30,
+ "supersecret".to_string().into_bytes(),
+ );
+ let time = SystemTime::now()
+ .duration_since(SystemTime::UNIX_EPOCH).unwrap()
+ .as_secs();
+ let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
+ println!("{}", url);
+ let token = totp.generate(time);
+ println!("{}", token);
+}