diff options
Diffstat (limited to 'src/pws.rs')
-rw-r--r-- | src/pws.rs | 26 |
1 files changed, 7 insertions, 19 deletions
@@ -1,8 +1,7 @@ use device::{Device, DeviceWrapper, Pro, Storage}; use libc; use nitrokey_sys; -use std::ffi::CString; -use util::{get_command_result, get_last_error, result_from_string, CommandError}; +use util::{get_command_result, get_cstring, get_last_error, result_from_string, CommandError}; /// The number of slots in a [`PasswordSafe`][]. /// @@ -42,7 +41,7 @@ pub const SLOT_COUNT: u8 = 16; /// let device = nitrokey::connect()?; /// let pws = device.get_password_safe("123456")?; /// use_password_safe(&pws); -/// device.lock(); +/// device.lock()?; /// # Ok(()) /// # } /// ``` @@ -87,7 +86,7 @@ pub trait GetPasswordSafe { /// match device.get_password_safe("123456") { /// Ok(pws) => { /// use_password_safe(&pws); - /// device.lock(); + /// device.lock()?; /// }, /// Err(err) => println!("Could not open the password safe: {:?}", err), /// }; @@ -106,11 +105,7 @@ fn get_password_safe<'a>( device: &'a Device, user_pin: &str, ) -> Result<PasswordSafe<'a>, CommandError> { - let user_pin_string = CString::new(user_pin); - if user_pin_string.is_err() { - return Err(CommandError::InvalidString); - } - let user_pin_string = user_pin_string.unwrap(); + let user_pin_string = get_cstring(user_pin)?; let result = unsafe { get_command_result(nitrokey_sys::NK_enable_password_safe( user_pin_string.as_ptr(), @@ -287,16 +282,9 @@ impl<'a> PasswordSafe<'a> { login: &str, password: &str, ) -> Result<(), CommandError> { - let name_string = CString::new(name); - let login_string = CString::new(login); - let password_string = CString::new(password); - if name_string.is_err() || login_string.is_err() || password_string.is_err() { - return Err(CommandError::InvalidString); - } - - let name_string = name_string.unwrap(); - let login_string = login_string.unwrap(); - let password_string = password_string.unwrap(); + let name_string = get_cstring(name)?; + let login_string = get_cstring(login)?; + let password_string = get_cstring(password)?; unsafe { get_command_result(nitrokey_sys::NK_write_password_safe_slot( slot, |