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

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

Returns the name of the given HOTP slot.

Errors
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),
};

Returns the name of the given TOTP slot.

Errors
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),
};

Generates an HOTP code on the given slot. This operation may require user authorization, depending on the device configuration (see get_config).

Errors
Example
use nitrokey::GenerateOtp;

let device = nitrokey::connect()?;
let code = device.get_hotp_code(1)?;
println!("Generated HOTP code on slot 1: {}", code);

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

Implementors