Skip to content

Commit

Permalink
password-hash: add Encoding::ShaCrypt (#1352)
Browse files Browse the repository at this point in the history
Adds support for a Base64 variant used by `sha1_crypt`, `sha256_crypt`,
`sha512_crypt`, and `md5_crypt`.

Uses `base64ct::Base64ShaCrypt` as the backing implementation.

Also marks the `Encoding` enum as `#[non_exhaustive]` to allow future additions
in a non-breaking manner.

---------

Co-authored-by: Tony Arcieri <[email protected]>
  • Loading branch information
SteelAlloy and tarcieri authored Jan 8, 2024
1 parent cbfdf1d commit fd48936
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion password-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ edition = "2021"
rust-version = "1.60"

[dependencies]
base64ct = "1"
base64ct = "1.6"
subtle = { version = "2", default-features = false }

# optional dependencies
Expand Down
16 changes: 15 additions & 1 deletion password-hash/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Base64 encoding variants.
use base64ct::{
Base64Bcrypt, Base64Crypt, Base64Unpadded as B64, Encoding as _, Error as B64Error,
Base64Bcrypt, Base64Crypt, Base64ShaCrypt, Base64Unpadded as B64, Encoding as _,
Error as B64Error,
};

/// Base64 encoding variants.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Encoding {
/// "B64" encoding: standard Base64 without padding.
///
Expand All @@ -31,6 +33,15 @@ pub enum Encoding {
/// 0x2e-0x39, 0x41-0x5a, 0x61-0x7a
/// ```
Crypt,

/// `crypt(3)` Base64 encoding for the following schemes.
/// - sha1_crypt,
/// - sha256_crypt,
/// - sha512_crypt,
/// - md5_crypt
/// [.-9] [A-Z] [a-z]
/// 0x2e-0x39, 0x41-0x5a, 0x61-0x7a
ShaCrypt,
}

impl Default for Encoding {
Expand All @@ -46,6 +57,7 @@ impl Encoding {
Self::B64 => B64::decode(src, dst),
Self::Bcrypt => Base64Bcrypt::decode(src, dst),
Self::Crypt => Base64Crypt::decode(src, dst),
Self::ShaCrypt => Base64ShaCrypt::decode(src, dst),
}
}

Expand All @@ -58,6 +70,7 @@ impl Encoding {
Self::B64 => B64::encode(src, dst),
Self::Bcrypt => Base64Bcrypt::encode(src, dst),
Self::Crypt => Base64Crypt::encode(src, dst),
Self::ShaCrypt => Base64ShaCrypt::encode(src, dst),
}
.map_err(Into::into)
}
Expand All @@ -68,6 +81,7 @@ impl Encoding {
Self::B64 => B64::encoded_len(bytes),
Self::Bcrypt => Base64Bcrypt::encoded_len(bytes),
Self::Crypt => Base64Crypt::encoded_len(bytes),
Self::ShaCrypt => Base64ShaCrypt::encoded_len(bytes),
}
}
}

0 comments on commit fd48936

Please sign in to comment.