Commit 32b618c827a056b432e1a1967d9ec9beb1c37ce2

Cleo Rebert 2020-06-21T15:57:16

Additional feature!

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);
+}