Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions pbkdf2/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ use sha2::{Sha256, Sha512};
#[cfg(feature = "sha1")]
use sha1::Sha1;

/// PBKDF2 (SHA-1)
#[cfg(feature = "sha1")]
pub const PBKDF2_SHA1: Ident = Ident::new_unwrap("pbkdf2");

/// PBKDF2 (SHA-256)
pub const PBKDF2_SHA256: Ident = Ident::new_unwrap("pbkdf2-sha256");

/// PBKDF2 (SHA-512)
pub const PBKDF2_SHA512: Ident = Ident::new_unwrap("pbkdf2-sha512");

/// PBKDF2 type for use with [`PasswordHasher`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(docsrs, doc(cfg(feature = "simple")))]
Expand All @@ -37,7 +27,7 @@ impl PasswordHasher for Pbkdf2 {
params: Params,
salt: impl Into<Salt<'a>>,
) -> Result<PasswordHash<'a>> {
let algorithm = Algorithm::try_from(alg_id.unwrap_or(PBKDF2_SHA256))?;
let algorithm = Algorithm::try_from(alg_id.unwrap_or(Algorithm::PBKDF2_SHA256_IDENT))?;

// Versions unsupported
if version.is_some() {
Expand Down Expand Up @@ -90,6 +80,16 @@ pub enum Algorithm {
}

impl Algorithm {
/// PBKDF2 (SHA-1) algorithm identifier
#[cfg(feature = "sha1")]
pub const PBKDF2_SHA1_IDENT: Ident<'static> = Ident::new_unwrap("pbkdf2");

/// PBKDF2 (SHA-256) algorithm identifier
pub const PBKDF2_SHA256_IDENT: Ident<'static> = Ident::new_unwrap("pbkdf2-sha256");

/// PBKDF2 (SHA-512) algorithm identifier
pub const PBKDF2_SHA512_IDENT: Ident<'static> = Ident::new_unwrap("pbkdf2-sha512");

/// Parse an [`Algorithm`] from the provided string.
pub fn new(id: impl AsRef<str>) -> Result<Self> {
id.as_ref().parse()
Expand All @@ -99,9 +99,9 @@ impl Algorithm {
pub fn ident(&self) -> Ident<'static> {
match self {
#[cfg(feature = "sha1")]
Algorithm::Pbkdf2Sha1 => PBKDF2_SHA1,
Algorithm::Pbkdf2Sha256 => PBKDF2_SHA256,
Algorithm::Pbkdf2Sha512 => PBKDF2_SHA512,
Algorithm::Pbkdf2Sha1 => Self::PBKDF2_SHA1_IDENT,
Algorithm::Pbkdf2Sha256 => Self::PBKDF2_SHA256_IDENT,
Algorithm::Pbkdf2Sha512 => Self::PBKDF2_SHA512_IDENT,
}
}

Expand Down Expand Up @@ -143,9 +143,9 @@ impl<'a> TryFrom<Ident<'a>> for Algorithm {
fn try_from(ident: Ident<'a>) -> Result<Algorithm> {
match ident {
#[cfg(feature = "sha1")]
PBKDF2_SHA1 => Ok(Algorithm::Pbkdf2Sha1),
PBKDF2_SHA256 => Ok(Algorithm::Pbkdf2Sha256),
PBKDF2_SHA512 => Ok(Algorithm::Pbkdf2Sha512),
Self::PBKDF2_SHA1_IDENT => Ok(Algorithm::Pbkdf2Sha1),
Self::PBKDF2_SHA256_IDENT => Ok(Algorithm::Pbkdf2Sha256),
Self::PBKDF2_SHA512_IDENT => Ok(Algorithm::Pbkdf2Sha512),
_ => Err(Error::Algorithm),
}
}
Expand Down