Struct nitrokey::PasswordSafe
source · pub struct PasswordSafe<'a> { /* private fields */ }
Expand description
A password safe on a Nitrokey device.
The password safe stores a tuple consisting of a name, a login and a password on a slot. The
number of available slots is SLOT_COUNT
. The slots are addressed starting with zero. To
retrieve a password safe from a Nitrokey device, use the get_password_safe
method from
the GetPasswordSafe
trait. Note that the device must live at least as long as the
password safe.
Once the password safe has been unlocked, it can be accessed without a password. Therefore it
is mandatory to call lock
on the corresponding device after the password store is used.
As this command may have side effects on the Nitrokey Storage, it cannot be called
automatically once the password safe is destroyed.
Examples
Open a password safe and access a password:
use nitrokey::{Device, GetPasswordSafe, PasswordSafe};
fn use_password_safe(pws: &PasswordSafe) -> Result<(), CommandError> {
let name = pws.get_slot_name(0)?;
let login = pws.get_slot_login(0)?;
let password = pws.get_slot_login(0)?;
println!("Credentials for {}: login {}, password {}", name, login, password);
Ok(())
}
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
use_password_safe(&pws);
device.lock()?;
Implementations
sourceimpl<'a> PasswordSafe<'a>
impl<'a> PasswordSafe<'a>
sourcepub fn get_slot_status(&self) -> Result<[bool; 16], CommandError>
pub fn get_slot_status(&self) -> Result<[bool; 16], CommandError>
Returns the status of all password slots.
The status indicates whether a slot is programmed or not.
Example
use nitrokey::{GetPasswordSafe, SLOT_COUNT};
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
pws.get_slot_status()?.iter().enumerate().for_each(|(slot, programmed)| {
let status = match *programmed {
true => "programmed",
false => "not programmed",
};
println!("Slot {}: {}", slot, status);
});
sourcepub fn get_slot_name(&self, slot: u8) -> Result<String, CommandError>
pub fn get_slot_name(&self, slot: u8) -> Result<String, CommandError>
Returns the name of the given slot (if it is programmed).
Errors
InvalidSlot
if the given slot is out of rangeUnknown
if the slot is not programmed
Example
use nitrokey::GetPasswordSafe;
let device = nitrokey::connect()?;
match device.get_password_safe("123456") {
Ok(pws) => {
let name = pws.get_slot_name(0)?;
let login = pws.get_slot_login(0)?;
let password = pws.get_slot_login(0)?;
println!("Credentials for {}: login {}, password {}", name, login, password);
},
Err(err) => println!("Could not open the password safe: {}", err),
};
sourcepub fn get_slot_login(&self, slot: u8) -> Result<String, CommandError>
pub fn get_slot_login(&self, slot: u8) -> Result<String, CommandError>
Returns the login for the given slot (if it is programmed).
Errors
InvalidSlot
if the given slot is out of rangeUnknown
if the slot is not programmed
Example
use nitrokey::GetPasswordSafe;
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
let name = pws.get_slot_name(0)?;
let login = pws.get_slot_login(0)?;
let password = pws.get_slot_login(0)?;
println!("Credentials for {}: login {}, password {}", name, login, password);
sourcepub fn get_slot_password(&self, slot: u8) -> Result<String, CommandError>
pub fn get_slot_password(&self, slot: u8) -> Result<String, CommandError>
Returns the password for the given slot (if it is programmed).
Errors
InvalidSlot
if the given slot is out of rangeUnknown
if the slot is not programmed
Example
use nitrokey::GetPasswordSafe;
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
let name = pws.get_slot_name(0)?;
let login = pws.get_slot_login(0)?;
let password = pws.get_slot_login(0)?;
println!("Credentials for {}: login {}, password {}", name, login, password);
sourcepub fn write_slot(
&self,
slot: u8,
name: &str,
login: &str,
password: &str
) -> Result<(), CommandError>
pub fn write_slot(
&self,
slot: u8,
name: &str,
login: &str,
password: &str
) -> Result<(), CommandError>
Writes the given slot with the given name, login and password.
Errors
InvalidSlot
if the given slot is out of rangeInvalidString
if the provided token ID contains a null byte
Example
use nitrokey::GetPasswordSafe;
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
let name = pws.get_slot_name(0)?;
let login = pws.get_slot_login(0)?;
let password = pws.get_slot_login(0)?;
println!("Credentials for {}: login {}, password {}", name, login, password);
sourcepub fn erase_slot(&self, slot: u8) -> Result<(), CommandError>
pub fn erase_slot(&self, slot: u8) -> Result<(), CommandError>
Erases the given slot. Erasing clears the stored name, login and password (if the slot was programmed).
Errors
InvalidSlot
if the given slot is out of range
Example
use nitrokey::GetPasswordSafe;
let device = nitrokey::connect()?;
let pws = device.get_password_safe("123456")?;
match pws.erase_slot(0) {
Ok(()) => println!("Erased slot 0."),
Err(err) => println!("Could not erase slot 0: {}", err),
};