Skip to main content

Password

Struct Password 

Source
pub struct Password(/* private fields */);
Expand description

A password used to unlock a crate::Keystore.

The password is stored in a Zeroizing<Vec<u8>>; the underlying memory is wiped when the Password is dropped. Password is Clone so callers may retain a copy for later use (e.g., re-locking with the same password); both copies are zeroized on drop.

§Construction

Use any of the From impls:

use dig_keystore::Password;

let from_str:     Password = Password::from("abc");
let from_string:  Password = Password::from(String::from("abc"));
let from_slice:   Password = Password::from(b"abc".as_slice());
let from_vec:     Password = Password::from(b"abc".to_vec());
let from_new:     Password = Password::new(b"abc");

§UTF-8 vs arbitrary bytes

Password accepts arbitrary byte sequences. Argon2id hashes raw bytes, so non-UTF-8 passwords work. The optional password-strength feature (which wires zxcvbn) requires UTF-8 and falls back to an empty-string score for non-UTF-8 bytes.

Implementations§

Source§

impl Password

Source

pub fn new(bytes: impl AsRef<[u8]>) -> Self

Wrap any byte sequence as a Password.

Copies the input bytes into a freshly-allocated zeroizing buffer. The caller’s original bytes are not wiped — callers managing particularly sensitive memory should zeroize the source themselves after passing it to Password::new.

Source

pub fn as_bytes(&self) -> &[u8]

Borrow the raw password bytes. The returned slice is valid only while the Password is alive.

Used internally by [crate::kdf] to feed Argon2id. External callers generally should not need this — prefer handing the Password to a Keystore method.

Source

pub fn len(&self) -> usize

Length in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the password is zero bytes long.

Empty passwords are permitted by the library (Argon2id will hash them), but they are trivially brute-forced. Treat as an operator error.

Trait Implementations§

Source§

impl Clone for Password

Source§

fn clone(&self) -> Password

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Password

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Never leaks password contents. Prints Password(<N bytes>).

A common mistake is to derive Debug on an outer struct that contains a Password, then log the struct and accidentally print the password. This custom Debug impl makes that mistake benign.

Source§

impl From<&[u8]> for Password

Source§

fn from(bytes: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Password

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Password

Source§

fn from(s: String) -> Self

Consumes the String, so the UTF-8 buffer is transferred into the zeroizing buffer with a single allocation and no leftover copy.

Source§

impl From<Vec<u8>> for Password

Source§

fn from(bytes: Vec<u8>) -> Self

Consumes the Vec<u8>, transferring ownership into the zeroizing buffer. Preferred over Password::new(&vec) because it avoids the double-allocation.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.