diff --git a/vnt/src/cipher/mod.rs b/vnt/src/cipher/mod.rs index 91a79a3..1cc12d7 100644 --- a/vnt/src/cipher/mod.rs +++ b/vnt/src/cipher/mod.rs @@ -29,3 +29,4 @@ mod aes_cbc; mod sm4_cbc; mod xor; +pub use xor::simple_hash; diff --git a/vnt/src/core/mod.rs b/vnt/src/core/mod.rs index d64248c..70fd235 100644 --- a/vnt/src/core/mod.rs +++ b/vnt/src/core/mod.rs @@ -147,33 +147,29 @@ 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]> { - self.password.as_ref().map(|v| { - use sha2::Digest; - let mut hasher = sha2::Sha256::new(); - hasher.update(self.cipher_model.to_string().as_bytes()); - hasher.update(v.as_bytes()); - hasher.update(self.token.as_bytes()); - let key: [u8; 32] = hasher.finalize().into(); - key[16..].try_into().unwrap() - }) - } - #[cfg(not(any( - feature = "aes_gcm", - feature = "server_encrypt", - feature = "aes_cbc", - feature = "aes_ecb", - feature = "sm4_cbc" - )))] - pub fn password_hash(&self) -> Option<[u8; 16]> { - None + 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; + let mut hasher = sha2::Sha256::new(); + hasher.update(self.cipher_model.to_string().as_bytes()); + hasher.update(p.as_bytes()); + hasher.update(self.token.as_bytes()); + let key: [u8; 32] = hasher.finalize().into(); + Some(key[16..].try_into().unwrap()) + } + } + } else { + None + } } }