Trait nitrokey::GenerateOtp
source · pub trait GenerateOtp {
fn set_time(&self, time: u64) -> Result<(), CommandError> { ... }
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError> { ... }
fn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError> { ... }
fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> { ... }
fn get_totp_code(&self, slot: u8) -> Result<String, CommandError> { ... }
}
Expand description
Provides methods to generate OTP codes and to query OTP slots on a Nitrokey device.
Provided Methods
sourcefn set_time(&self, time: u64) -> Result<(), CommandError>
fn set_time(&self, time: u64) -> Result<(), CommandError>
Sets the time on the Nitrokey. This command may set the time to arbitrary values. time
is the number of seconds since January 1st, 1970 (Unix timestamp).
The time is used for TOTP generation (see get_totp_code
).
Example
extern crate chrono;
use chrono::Utc;
use nitrokey::Device;
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);
}
Errors
Timestamp
if the time could not be set
sourcefn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError>
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError>
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, GenerateOtp};
let device = nitrokey::connect()?;
match device.get_hotp_slot_name(1) {
Ok(name) => println!("HOTP slot 1: {}", name),
Err(CommandError::SlotNotProgrammed) => println!("HOTP slot 1 not programmed"),
Err(err) => println!("Could not get slot name: {}", err),
};
sourcefn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError>
fn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError>
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, GenerateOtp};
let device = nitrokey::connect()?;
match device.get_totp_slot_name(1) {
Ok(name) => println!("TOTP slot 1: {}", name),
Err(CommandError::SlotNotProgrammed) => println!("TOTP slot 1 not programmed"),
Err(err) => println!("Could not get slot name: {}", err),
};
sourcefn get_hotp_code(&self, slot: u8) -> Result<String, CommandError>
fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError>
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 device = nitrokey::connect()?;
let code = device.get_hotp_code(1)?;
println!("Generated HOTP code on slot 1: {}", code);
sourcefn get_totp_code(&self, slot: u8) -> Result<String, CommandError>
fn get_totp_code(&self, slot: u8) -> Result<String, CommandError>
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
extern crate chrono;
use nitrokey::GenerateOtp;
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);
let code = device.get_totp_code(1)?;
println!("Generated TOTP code on slot 1: {}", code);
}