diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-22 09:11:27 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-22 11:12:12 +0200 |
commit | ea00d06fc614c632a4c7250ef0c769705ed8c1c3 (patch) | |
tree | 804332537c8749239b16072a21452d85eab8dcde | |
parent | 2c31912bd2f8731c146da6640e28896c9de6286a (diff) | |
download | nitrokey-rs-ea00d06fc614c632a4c7250ef0c769705ed8c1c3.tar.gz nitrokey-rs-ea00d06fc614c632a4c7250ef0c769705ed8c1c3.tar.bz2 |
Add an example for setting the current time
The example uses the chrono crate and can therefore not be executed, but
at least it shows how to set the Nitrokey device to the current time.
-rw-r--r-- | TODO.md | 1 | ||||
-rw-r--r-- | src/device.rs | 22 | ||||
-rw-r--r-- | src/otp.rs | 14 |
3 files changed, 32 insertions, 5 deletions
@@ -36,7 +36,6 @@ - `NK_list_devices_by_cpuID` - `NK_connect_with_ID` - Fix timing issues with the `totp` and `totp_pin` test cases. -- Find an example for `set_time`, also adapt `get_totp_code`. - Improve log level documentation. - Clear passwords from memory. - Find a nicer syntax for the `write_config` test. diff --git a/src/device.rs b/src/device.rs index 6c1a957..ce45a50 100644 --- a/src/device.rs +++ b/src/device.rs @@ -99,13 +99,33 @@ pub trait Device { /// /// The time is used for TOTP generation (see [`get_totp_code`][]). /// + /// # Example + /// + /// ```ignore + /// extern crate chrono; + /// + /// use chrono::Utc; + /// use nitrokey::Device; + /// # use nitrokey::CommandError; + /// + /// # fn try_main() -> Result<(), CommandError> { + /// let device = nitrokey::connect()?; + /// let time = Utc::now().timestamp(); + /// if time < 0 { + /// println!("Timestamps before 1970-01-01 are not supported!"); + /// } else { + /// device.set_time(time as u64); + /// } + /// # Ok(()) + /// # } + /// ``` + /// /// # Errors /// /// - [`Timestamp`][] if the time could not be set /// /// [`get_totp_code`]: trait.ProvideOtp.html#method.get_totp_code /// [`Timestamp`]: enum.CommandError.html#variant.Timestamp - // TODO: example fn set_time(&self, time: u64) -> CommandStatus { unsafe { CommandStatus::from(nitrokey_sys::NK_totp_set_time(time)) } } @@ -256,14 +256,22 @@ pub trait GenerateOtp { /// /// # Example /// - /// ```no_run + /// ```ignore + /// extern crate chrono; + /// /// use nitrokey::GenerateOtp; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), CommandError> { /// let device = nitrokey::connect()?; - /// let code = device.get_totp_code(1)?; - /// println!("Generated TOTP code on slot 1: {:?}", code); + /// let time = Utc::now().timestamp(); + /// if time < 0 { + /// println!("Timestamps before 1970-01-01 are not supported!"); + /// } else { + /// device.set_time(time as u64); + /// let code = device.get_totp_code(1)?; + /// println!("Generated TOTP code on slot 1: {:?}", code); + /// } /// # Ok(()) /// # } /// ``` |