refact(password): decode preset password

This commit is contained in:
fufesou
2026-05-20 10:53:26 +08:00
parent 3f30a92913
commit cf108c131e

View File

@@ -145,7 +145,14 @@ pub fn preset_permanent_password_storage_is_usable_for_auth(storage: &str, salt:
if salt.is_empty() { if salt.is_empty() {
return true; return true;
} }
decode_permanent_password_h1_from_storage(storage).is_some() decode_preset_password_h1_from_storage(storage).is_some()
}
fn decode_preset_password_h1_from_storage(
storage: &str,
) -> Option<[u8; PERMANENT_PASSWORD_H1_LEN]> {
decode_permanent_password_h1_from_storage(storage)
.or_else(|| decode_hbbs_preset_password_h1_from_storage(storage))
} }
pub fn local_permanent_password_storage_is_usable_for_auth(storage: &str, salt: &str) -> bool { pub fn local_permanent_password_storage_is_usable_for_auth(storage: &str, salt: &str) -> bool {
@@ -194,7 +201,7 @@ pub(super) fn preset_permanent_password_storage_matches_plain(
if salt.is_empty() { if salt.is_empty() {
return storage == input; return storage == input;
} }
let Some(stored_h1) = decode_permanent_password_h1_from_storage(storage) else { let Some(stored_h1) = decode_preset_password_h1_from_storage(storage) else {
return false; return false;
}; };
let h1 = compute_permanent_password_h1(input, salt); let h1 = compute_permanent_password_h1(input, salt);
@@ -250,7 +257,7 @@ mod tests {
let password = "p@ssw0rd"; let password = "p@ssw0rd";
let h1 = compute_permanent_password_h1(password, salt); let h1 = compute_permanent_password_h1(password, salt);
let stored = encode_permanent_password_storage_from_h1(&h1); let stored = encode_permanent_password_storage_from_h1(&h1);
assert!(stored.starts_with(HBBS_PRESET_PASSWORD_HASH_PREFIX)); assert!(stored.starts_with(PERMANENT_PASSWORD_HASH_PREFIX));
assert!(is_permanent_password_hashed_storage(&stored)); assert!(is_permanent_password_hashed_storage(&stored));
let decoded = decode_permanent_password_h1_from_hashed_storage(&stored).unwrap(); let decoded = decode_permanent_password_h1_from_hashed_storage(&stored).unwrap();
assert_eq!(&decoded[..], &h1[..]); assert_eq!(&decoded[..], &h1[..]);
@@ -302,6 +309,23 @@ mod tests {
); );
} }
#[test]
fn test_hbbs_00_hashed_preset_password_storage_matches_plain_with_salt() {
let salt = "salt123";
let h1 = compute_permanent_password_h1("p@ssw0rd", salt);
let storage = encode_hbbs_preset_password_storage_from_h1(&h1);
assert!(preset_permanent_password_storage_is_usable_for_auth(
&storage, salt
));
assert!(preset_permanent_password_storage_matches_plain(
&storage, salt, "p@ssw0rd"
));
assert!(!preset_permanent_password_storage_matches_plain(
&storage, salt, "wrong"
));
}
#[test] #[test]
fn test_hbbs_00_shaped_preset_password_without_salt_stays_plaintext() { fn test_hbbs_00_shaped_preset_password_without_salt_stays_plaintext() {
let h1 = compute_permanent_password_h1("p@ssw0rd", "salt123"); let h1 = compute_permanent_password_h1("p@ssw0rd", "salt123");