refactor: ekey decrypt now return Vec<u8> instead.

This commit is contained in:
鲁树人 2024-09-15 16:18:18 +01:00
parent e1ed4ba6cf
commit 7502b310d2

View File

@ -42,7 +42,7 @@ fn make_simple_key<const N: usize>() -> [u8; N] {
result result
} }
pub fn decrypt_v1(ekey: &[u8]) -> Result<Box<[u8]>> { pub fn decrypt_v1(ekey: &[u8]) -> Result<Vec<u8>> {
if ekey.len() < 12 { if ekey.len() < 12 {
Err(EKeyDecryptError::EKeyTooShort)?; Err(EKeyDecryptError::EKeyTooShort)?;
} }
@ -58,10 +58,10 @@ pub fn decrypt_v1(ekey: &[u8]) -> Result<Box<[u8]>> {
.collect_vec(); .collect_vec();
let plaintext = tc_tea::decrypt(cipher, tea_key).map_err(EKeyDecryptError::FailDecryptV1)?; let plaintext = tc_tea::decrypt(cipher, tea_key).map_err(EKeyDecryptError::FailDecryptV1)?;
Ok([header, &plaintext].concat().into()) Ok([header, &plaintext].concat())
} }
pub fn decrypt_v2(ekey: &[u8]) -> Result<Box<[u8]>> { pub fn decrypt_v2(ekey: &[u8]) -> Result<Vec<u8>> {
let ekey = base64::decode(ekey)?; let ekey = base64::decode(ekey)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY1).map_err(EKeyDecryptError::FailDecryptV2)?; let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY1).map_err(EKeyDecryptError::FailDecryptV2)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY2).map_err(EKeyDecryptError::FailDecryptV2)?; let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY2).map_err(EKeyDecryptError::FailDecryptV2)?;
@ -70,7 +70,7 @@ pub fn decrypt_v2(ekey: &[u8]) -> Result<Box<[u8]>> {
decrypt_v1(&ekey) decrypt_v1(&ekey)
} }
pub fn decrypt<T: AsRef<[u8]>>(ekey: T) -> Result<Box<[u8]>> { pub fn decrypt<T: AsRef<[u8]>>(ekey: T) -> Result<Vec<u8>> {
let ekey = ekey.as_ref(); let ekey = ekey.as_ref();
match ekey.strip_prefix(EKEY_V2_PREFIX) { match ekey.strip_prefix(EKEY_V2_PREFIX) {
Some(v2_ekey) => decrypt_v2(v2_ekey), Some(v2_ekey) => decrypt_v2(v2_ekey),