Struct PasswordHash
pub struct PasswordHash { /* private fields */ }
dep_orion
only.Expand description
A type to represent the PasswordHash
that Argon2i returns when used for password hashing.
§Errors:
An error will be returned if:
- The encoded password hash contains whitespace.
- The encoded password hash has a parallelism count other than 1.
- The encoded password contains any other fields than: The algorithm name, version, m, t, p and the salt and password hash.
- The encoded password hash contains invalid Base64 encoding.
- Any decimal parameter value, such as m, contains leading zeroes and is longer than a single character.
iterations
is less than 3.memory
is less than 8.password
is not 32 bytes.salt
is not 16 bytes.- The encoded password hash contains numerical values that cannot
be represented as a
u32
. - The encoded password hash length is less than
PasswordHash::MIN_ENCODED_LEN
or greater thanPasswordHash::MAX_ENCODED_LEN
. - The parameters in the encoded password hash are not correctly ordered. The ordering must be:
$argon2i$v=19$m=<value>,t=<value>,p=<value>$<salt>$<hash>
§Panics:
A panic will occur if:
- Overflowing calculations happen on
usize
when decoding the password and salt from Base64.
§Security:
- Avoid using
unprotected_as_bytes()
whenever possible, as it breaks all protections that the type implements. - Never use
unprotected_as_bytes()
orunprotected_as_encoded()
to compare password hashes, as that will not run in constant-time. ComparePasswordHash
es directly using==
instead. - The trait
PartialEq<&'_ [u8]>
is implemented for this type so that users are not tempted to callunprotected_as_bytes
to compare this sensitive value to a byte slice. The trait is implemented in such a way that the comparison happens in constant time. Thus, users should preferSecretType == &[u8]
overSecretType.unprotected_as_bytes() == &[u8]
.
Examples are shown below. The examples apply to any type that implements PartialEq<&'_ [u8]>
.
use orion::hazardous::mac::hmac::sha512::Tag;
// Initialize an arbitrary, 64-byte tag.
let tag = Tag::from_slice(&[1; 64])?;
// Secure, constant-time comparison with a byte slice
assert_eq!(tag, &[1; 64][..]);
// Secure, constant-time comparison with another Tag
assert_eq!(tag, Tag::from_slice(&[1; 64])?);
Implementations§
§impl PasswordHash
impl PasswordHash
pub const MIN_ENCODED_LEN: usize = 92usize
pub const MIN_ENCODED_LEN: usize = 92usize
Given a 16-byte salt (22 characters encoded) and 32-byte password hash (43 characters encoded), and parameters (m, t) in decimal representation of 1..10 in length, 92 is the minimum length for an encoded password hash.
pub const MAX_ENCODED_LEN: usize = 110usize
pub const MAX_ENCODED_LEN: usize = 110usize
Given a 16-byte salt (22 characters encoded) and 32-byte password hash (43 characters encoded), and parameters (m, t) in decimal representation of 1..10 in length, 110 is the maximum length for an encoded password hash.
pub fn from_slice(
password_hash: &[u8],
salt: &[u8],
iterations: u32,
memory: u32,
) -> Result<PasswordHash, UnknownCryptoError> ⓘ
pub fn from_slice( password_hash: &[u8], salt: &[u8], iterations: u32, memory: u32, ) -> Result<PasswordHash, UnknownCryptoError> ⓘ
Construct from given byte slice and parameters.
pub fn from_encoded(
password_hash: &str,
) -> Result<PasswordHash, UnknownCryptoError> ⓘ
pub fn from_encoded( password_hash: &str, ) -> Result<PasswordHash, UnknownCryptoError> ⓘ
Construct from encoded password hash.
pub fn unprotected_as_encoded(&self) -> &str ⓘ
pub fn unprotected_as_encoded(&self) -> &str ⓘ
Return encoded password hash. Warning: Should not be used to verify password hashes. This breaks protections that the type implements.
pub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
pub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
Return the password hash as byte slice. Warning: Should not be used unless strictly needed. This breaks protections that the type implements.
Trait Implementations§
§impl Debug for PasswordHash
impl Debug for PasswordHash
§impl<'de> Deserialize<'de> for PasswordHash
PasswordHash
deserializes from a String
.
impl<'de> Deserialize<'de> for PasswordHash
PasswordHash
deserializes from a String
.
§fn deserialize<D>(
deserializer: D,
) -> Result<PasswordHash, <D as Deserializer<'de>>::Error> ⓘwhere
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<PasswordHash, <D as Deserializer<'de>>::Error> ⓘwhere
D: Deserializer<'de>,
§impl PartialEq<&[u8]> for PasswordHash
impl PartialEq<&[u8]> for PasswordHash
§impl PartialEq for PasswordHash
impl PartialEq for PasswordHash
§impl Serialize for PasswordHash
PasswordHash
serializes as would a String
. Note that
the serialized type likely does not have the same protections that Orion
provides, such as constant-time operations. A good rule of thumb is to only
serialize these types for storage. Don’t operate on the serialized types.
impl Serialize for PasswordHash
PasswordHash
serializes as would a String
. Note that
the serialized type likely does not have the same protections that Orion
provides, such as constant-time operations. A good rule of thumb is to only
serialize these types for storage. Don’t operate on the serialized types.
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> ⓘwhere
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> ⓘwhere
S: Serializer,
impl Eq for PasswordHash
Auto 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> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId
of Self
using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§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 more