[−][src]Trait nitrokey::GenerateOtp
Provides methods to generate OTP codes and to query OTP slots on a Nitrokey device.
Provided methods
fn set_time(&mut self, time: u64, force: bool) -> Result<(), Error>
Sets the time on the Nitrokey.
time
is the number of seconds since January 1st, 1970 (Unix timestamp). Unless force
is set to true
, this command fails if the timestamp on the device is larger than the
given timestamp or if it is zero.
The time is used for TOTP generation (see get_totp_code
).
Example
use std::time; use nitrokey::GenerateOtp; let mut manager = nitrokey::take()?; let mut device = manager.connect()?; let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH); match time { Ok(time) => device.set_time(time.as_secs(), false)?, Err(_) => eprintln!("The system time is before the Unix epoch!"), }
Errors
Timestamp
if the time could not be set
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, Error>
Returns the name of the given HOTP slot.
Errors
InvalidSlot
if there is no slot with the given numberSlotNotProgrammed
if the given slot is not configured
Example
use nitrokey::{CommandError, Error, GenerateOtp}; let mut manager = nitrokey::take()?; let device = manager.connect()?; match device.get_hotp_slot_name(1) { Ok(name) => println!("HOTP slot 1: {}", name), Err(Error::CommandError(CommandError::SlotNotProgrammed)) => eprintln!("HOTP slot 1 not programmed"), Err(err) => eprintln!("Could not get slot name: {}", err), };
fn get_totp_slot_name(&self, slot: u8) -> Result<String, Error>
Returns the name of the given TOTP slot.
Errors
InvalidSlot
if there is no slot with the given numberSlotNotProgrammed
if the given slot is not configured
Example
use nitrokey::{CommandError, Error, GenerateOtp}; let mut manager = nitrokey::take()?; let device = manager.connect()?; match device.get_totp_slot_name(1) { Ok(name) => println!("TOTP slot 1: {}", name), Err(Error::CommandError(CommandError::SlotNotProgrammed)) => eprintln!("TOTP slot 1 not programmed"), Err(err) => eprintln!("Could not get slot name: {}", err), };
fn get_hotp_code(&mut self, slot: u8) -> Result<String, Error>
Generates an HOTP code on the given slot. This operation may require user authorization,
depending on the device configuration (see get_config
).
Errors
InvalidSlot
if there is no slot with the given numberNotAuthorized
if OTP generation requires user authenticationSlotNotProgrammed
if the given slot is not configured
Example
use nitrokey::GenerateOtp; let mut manager = nitrokey::take()?; let mut device = manager.connect()?; let code = device.get_hotp_code(1)?; println!("Generated HOTP code on slot 1: {}", code);
fn get_totp_code(&self, slot: u8) -> Result<String, Error>
Generates a TOTP code on the given slot. This operation may require user authorization,
depending on the device configuration (see get_config
).
To make sure that the Nitrokey’s time is in sync, consider calling set_time
before
calling this method.
Errors
InvalidSlot
if there is no slot with the given numberNotAuthorized
if OTP generation requires user authenticationSlotNotProgrammed
if the given slot is not configured
Example
use std::time; use nitrokey::GenerateOtp; let mut manager = nitrokey::take()?; let mut device = manager.connect()?; let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH); match time { Ok(time) => { device.set_time(time.as_secs(), false)?; let code = device.get_totp_code(1)?; println!("Generated TOTP code on slot 1: {}", code); }, Err(_) => eprintln!("Timestamps before 1970-01-01 are not supported!"), }