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
impl PasswordHash
Sourcepub fn new<T: Into<String>>(hash: T) -> Result<Self>
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.
Sourcepub fn from_password(password: &Password) -> Self
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"));Sourcepub fn verify(&self, password: &Password) -> PasswordVerificationResult
pub fn verify(&self, password: &Password) -> PasswordVerificationResult
Verifies a password against the hash.
- If the password is valid, returns
PasswordVerificationResult::Ok. - If the password is valid but the hash is obsolete, returns
PasswordVerificationResult::OkObsoletewith the new hash calculated with the currently preferred algorithm. The new hash should be saved to the database. - If the password is invalid, returns
PasswordVerificationResult::Invalid.
§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"),
}Sourcepub fn as_str(&self) -> &str
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());Sourcepub fn into_string(self) -> String
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
impl AsFormField for PasswordHash
Source§type Type = PasswordField
type Type = PasswordField
Source§fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>
fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>
Source§fn to_field_value(&self) -> String
fn to_field_value(&self) -> String
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
fn new_field( options: FormFieldOptions, custom_options: <Self::Type as FormField>::CustomOptions, ) -> Self::Type
Source§impl Clone for PasswordHash
impl Clone for PasswordHash
Source§fn clone(&self) -> PasswordHash
fn clone(&self) -> PasswordHash
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl DatabaseField for PasswordHash
Available on crate feature db only.
impl DatabaseField for PasswordHash
db only.Source§impl Debug for PasswordHash
impl Debug for PasswordHash
Source§impl FromDbValue for PasswordHash
Available on crate feature db only.
impl FromDbValue for PasswordHash
db only.Source§fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>
fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>
sqlite only.Source§fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>
fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>
postgres only.Source§fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>where
Self: Sized,
fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>where
Self: Sized,
mysql only.Source§impl ToDbValue for PasswordHash
Available on crate feature db only.
impl ToDbValue for PasswordHash
db only.Source§fn to_db_value(&self) -> DbValue
fn to_db_value(&self) -> DbValue
sea_query value. Read moreAuto Trait Implementations§
impl Freeze for PasswordHash
impl RefUnwindSafe for PasswordHash
impl Send for PasswordHash
impl Sync for PasswordHash
impl Unpin for PasswordHash
impl UnwindSafe for PasswordHash
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.Source§impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
Source§fn into_field(self) -> T
fn into_field(self) -> T
db only.Source§impl<T> ToDbFieldValue for Twhere
T: ToDbValue,
impl<T> ToDbFieldValue for Twhere
T: ToDbValue,
Source§fn to_db_field_value(&self) -> DbFieldValue
fn to_db_field_value(&self) -> DbFieldValue
db only.DbFieldValue that indicates whether
the value should be automatically generated by the database, or
contains a specific, explicitly provided value.