计算密码hash

This commit is contained in:
lbl8603
2024-05-28 23:21:43 +08:00
parent 5eed05989f
commit 90ff03e27c
2 changed files with 23 additions and 26 deletions
+1
View File
@@ -29,3 +29,4 @@ mod aes_cbc;
mod sm4_cbc; mod sm4_cbc;
mod xor; mod xor;
pub use xor::simple_hash;
+15 -19
View File
@@ -147,33 +147,29 @@ impl Config {
}) })
} }
} }
impl Config { impl Config {
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub fn password_hash(&self) -> Option<[u8; 16]> { pub fn password_hash(&self) -> Option<[u8; 16]> {
self.password.as_ref().map(|v| { if let Some(p) = self.password.as_ref() {
match self.cipher_model {
CipherModel::Xor => {
let key = crate::cipher::simple_hash(&format!("Xor{}{}", p, self.token));
Some(key[16..].try_into().unwrap())
}
CipherModel::None => None,
#[cfg(cipher)]
_ => {
use sha2::Digest; use sha2::Digest;
let mut hasher = sha2::Sha256::new(); let mut hasher = sha2::Sha256::new();
hasher.update(self.cipher_model.to_string().as_bytes()); hasher.update(self.cipher_model.to_string().as_bytes());
hasher.update(v.as_bytes()); hasher.update(p.as_bytes());
hasher.update(self.token.as_bytes()); hasher.update(self.token.as_bytes());
let key: [u8; 32] = hasher.finalize().into(); let key: [u8; 32] = hasher.finalize().into();
key[16..].try_into().unwrap() Some(key[16..].try_into().unwrap())
})
} }
#[cfg(not(any( }
feature = "aes_gcm", } else {
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
pub fn password_hash(&self) -> Option<[u8; 16]> {
None None
} }
} }
}