优化提示
This commit is contained in:
+19
-15
@@ -122,7 +122,7 @@ impl Cipher {
|
|||||||
model: CipherModel,
|
model: CipherModel,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
token: Option<String>,
|
token: Option<String>,
|
||||||
) -> Self {
|
) -> anyhow::Result<Self> {
|
||||||
if let Some(password) = password {
|
if let Some(password) = password {
|
||||||
#[cfg(cipher)]
|
#[cfg(cipher)]
|
||||||
let key: [u8; 32] = {
|
let key: [u8; 32] = {
|
||||||
@@ -136,33 +136,33 @@ impl Cipher {
|
|||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
if password.len() < 8 {
|
if password.len() < 8 {
|
||||||
let aes = AesGcmCipher::new_128(key[..16].try_into().unwrap(), finger);
|
let aes = AesGcmCipher::new_128(key[..16].try_into().unwrap(), finger);
|
||||||
Cipher::AesGcm((aes, key[..16].to_vec()))
|
Ok(Cipher::AesGcm((aes, key[..16].to_vec())))
|
||||||
} else {
|
} else {
|
||||||
let aes = AesGcmCipher::new_256(key, finger);
|
let aes = AesGcmCipher::new_256(key, finger);
|
||||||
Cipher::AesGcm((aes, key.to_vec()))
|
Ok(Cipher::AesGcm((aes, key.to_vec())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "chacha20_poly1305")]
|
#[cfg(feature = "chacha20_poly1305")]
|
||||||
CipherModel::Chacha20Poly1305 => {
|
CipherModel::Chacha20Poly1305 => {
|
||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
let chacha = ChaCha20Poly1305Cipher::new_256(key, finger);
|
let chacha = ChaCha20Poly1305Cipher::new_256(key, finger);
|
||||||
Cipher::Chacha20Poly1305(chacha)
|
Ok(Cipher::Chacha20Poly1305(chacha))
|
||||||
}
|
}
|
||||||
#[cfg(feature = "chacha20_poly1305")]
|
#[cfg(feature = "chacha20_poly1305")]
|
||||||
CipherModel::Chacha20 => {
|
CipherModel::Chacha20 => {
|
||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
let chacha = ChaCha20Cipher::new_256(key, finger);
|
let chacha = ChaCha20Cipher::new_256(key, finger);
|
||||||
Cipher::Chacha20(chacha)
|
Ok(Cipher::Chacha20(chacha))
|
||||||
}
|
}
|
||||||
#[cfg(feature = "aes_cbc")]
|
#[cfg(feature = "aes_cbc")]
|
||||||
CipherModel::AesCbc => {
|
CipherModel::AesCbc => {
|
||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
if password.len() < 8 {
|
if password.len() < 8 {
|
||||||
let aes = AesCbcCipher::new_128(key[..16].try_into().unwrap(), finger);
|
let aes = AesCbcCipher::new_128(key[..16].try_into().unwrap(), finger);
|
||||||
Cipher::AesCbc(aes)
|
Ok(Cipher::AesCbc(aes))
|
||||||
} else {
|
} else {
|
||||||
let aes = AesCbcCipher::new_256(key, finger);
|
let aes = AesCbcCipher::new_256(key, finger);
|
||||||
Cipher::AesCbc(aes)
|
Ok(Cipher::AesCbc(aes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "aes_ecb")]
|
#[cfg(feature = "aes_ecb")]
|
||||||
@@ -170,28 +170,32 @@ impl Cipher {
|
|||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
if password.len() < 8 {
|
if password.len() < 8 {
|
||||||
let aes = AesEcbCipher::new_128(key[..16].try_into().unwrap(), finger);
|
let aes = AesEcbCipher::new_128(key[..16].try_into().unwrap(), finger);
|
||||||
Cipher::AesEcb(aes)
|
Ok(Cipher::AesEcb(aes))
|
||||||
} else {
|
} else {
|
||||||
let aes = AesEcbCipher::new_256(key, finger);
|
let aes = AesEcbCipher::new_256(key, finger);
|
||||||
Cipher::AesEcb(aes)
|
Ok(Cipher::AesEcb(aes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "sm4_cbc")]
|
#[cfg(feature = "sm4_cbc")]
|
||||||
CipherModel::Sm4Cbc => {
|
CipherModel::Sm4Cbc => {
|
||||||
let finger = token.map(|token| Finger::new(&token));
|
let finger = token.map(|token| Finger::new(&token));
|
||||||
let aes = Sm4CbcCipher::new_128(key[..16].try_into().unwrap(), finger);
|
let aes = Sm4CbcCipher::new_128(key[..16].try_into().unwrap(), finger);
|
||||||
Cipher::Sm4Cbc(aes)
|
Ok(Cipher::Sm4Cbc(aes))
|
||||||
}
|
}
|
||||||
CipherModel::Xor => {
|
CipherModel::Xor => {
|
||||||
let _token = token;
|
if token.is_some() {
|
||||||
Cipher::Xor(XORCipher::new_256(crate::cipher::xor::simple_hash(
|
Err(anyhow::anyhow!(
|
||||||
&password,
|
"'finger' and 'xor' cannot be used simultaneously"
|
||||||
|
))?
|
||||||
|
}
|
||||||
|
Ok(Cipher::Xor(XORCipher::new_256(
|
||||||
|
crate::cipher::xor::simple_hash(&password),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
CipherModel::None => Cipher::None,
|
CipherModel::None => Ok(Cipher::None),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Cipher::None
|
Ok(Cipher::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(not(any(feature = "aes_gcm", feature = "server_encrypt")))]
|
#[cfg(not(any(feature = "aes_gcm", feature = "server_encrypt")))]
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ impl Vnt {
|
|||||||
};
|
};
|
||||||
//客户端对称加密
|
//客户端对称加密
|
||||||
let client_cipher =
|
let client_cipher =
|
||||||
Cipher::new_password(config.cipher_model, config.password.clone(), finger);
|
Cipher::new_password(config.cipher_model, config.password.clone(), finger)?;
|
||||||
//当前设备信息
|
//当前设备信息
|
||||||
let current_device = Arc::new(AtomicCell::new(CurrentDeviceInfo::new0(
|
let current_device = Arc::new(AtomicCell::new(CurrentDeviceInfo::new0(
|
||||||
config.server_address,
|
config.server_address,
|
||||||
|
|||||||
Reference in New Issue
Block a user