Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

password-hash: add Base64ShaCrypt #1352

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
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
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),
}
}
}