Struct PasswordHash

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

A hashed password.

This is used to store a hashed user password in the database. The password hash is created using the password_auth crate internally, so by default, it generates the hash using the latest recommended algorithm.

§Security

The implementation of the Debug trait for this type hides the password hash value to prevent it from being leaked in logs or other debug output.

There is no PartialEq implementation for this type, as it should never be needed to compare password hashes directly. Instead, use the verify method to verify a password against the hash.

Implementations§

Source§

impl PasswordHash

Source

pub fn new<T: Into<String>>(hash: T) -> Result<Self>

Creates a new password hash object from a string.

Note that this method takes the hash directly. If you need to hash a password, use PasswordHash::from_password instead.

§Examples
use cot::auth::{Password, PasswordHash};

let hash = PasswordHash::from_password(&Password::new("password"));
let stored_hash = hash.into_string();
let hash = PasswordHash::new(stored_hash).unwrap();
§Errors

Returns an error if the password hash is invalid.

Source

pub fn from_password(password: &Password) -> Self

Creates a new password hash from a password.

The password is hashed using the latest recommended algorithm.

§Examples
use cot::auth::{Password, PasswordHash};

let hash = PasswordHash::from_password(&Password::new("password"));
Source

pub fn verify(&self, password: &Password) -> PasswordVerificationResult

Verifies a password against the hash.

§Examples
use cot::auth::{Password, PasswordHash, PasswordVerificationResult};

let password = Password::new("password");
let hash = PasswordHash::from_password(&password);

match hash.verify(&password) {
    PasswordVerificationResult::Ok => println!("Password is valid"),
    PasswordVerificationResult::OkObsolete(new_hash) => println!(
        "Password is valid, but the hash is obsolete. Save the new hash: {}",
        new_hash.as_str()
    ),
    PasswordVerificationResult::Invalid => println!("Password is invalid"),
}
Source

pub fn as_str(&self) -> &str

Returns the password hash as a string.

For security reasons, you should avoid using this method as much as possible. Typically, you should use the PasswordHash::verify() method to verify a password against the hash. This method is mostly useful for persisting the password hash externally.

§Examples
use cot::auth::{Password, PasswordHash};

let hash = PasswordHash::from_password(&Password::new("password"));
assert!(!hash.as_str().is_empty());
Source

pub fn into_string(self) -> String

Consumes the object and returns the password hash as a string.

For security reasons, you should avoid using this method as much as possible. Typically, you should use the PasswordHash::verify() method to verify a password against the hash. This method is mostly useful for persisting the password hash externally.

§Examples
use cot::auth::{Password, PasswordHash};

let hash = PasswordHash::from_password(&Password::new("password"));
assert!(!hash.into_string().is_empty());

Trait Implementations§

Source§

impl AsFormField for PasswordHash

Source§

type Type = PasswordField

The form field type associated with the field.
Source§

fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>

Validates the value of the field and converts it to the final type. This method should return an error if the value is invalid. Read more
Source§

fn to_field_value(&self) -> String

Returns self as a value that can be set with FormField::set_value.
Source§

fn new_field( options: FormFieldOptions, custom_options: <Self::Type as FormField>::CustomOptions, ) -> Self::Type

Creates a new form field with the given options and custom options. Read more
Source§

impl Clone for PasswordHash

Source§

fn clone(&self) -> PasswordHash

Returns a copy 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 DatabaseField for PasswordHash

Available on crate feature db only.
Source§

const TYPE: ColumnType

The type of the column in the database as one of the variants of the ColumnType enum. Read more
Source§

const NULLABLE: bool = false

Whether the field can be NULL in the database. Read more
Source§

impl Debug for PasswordHash

Source§

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

Formats the value using the given formatter. Read more
Source§

impl FromDbValue for PasswordHash

Available on crate feature db only.
Source§

fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>

Available on crate feature sqlite only.
Converts the given SQLite database value to a Rust value. Read more
Source§

fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>

Available on crate feature postgres only.
Converts the given PostgreSQL database value to a Rust value. Read more
Source§

fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>
where Self: Sized,

Available on crate feature mysql only.
Converts the given MySQL database value to a Rust value. Read more
Source§

impl ToDbValue for PasswordHash

Available on crate feature db only.
Source§

fn to_db_value(&self) -> DbValue

Converts the Rust value to a sea_query value. Read more
Source§

impl TryFrom<String> for PasswordHash

Source§

type Error = AuthError

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.

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, dst: *mut u8)

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

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoField<Auto<T>> for T

Source§

fn into_field(self) -> Auto<T>

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> IntoField<T> for T
where T: ToDbFieldValue,

Source§

fn into_field(self) -> T

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToDbFieldValue for T
where T: ToDbValue,

Source§

fn to_db_field_value(&self) -> DbFieldValue

Available on crate feature db only.
Converts the Rust value to a DbFieldValue that indicates whether the value should be automatically generated by the database, or contains a specific, explicitly provided value.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> MaybeSendSync for T