mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-07 20:19:51 +00:00
[kwm] refactor: move Decipher new fn under struct
This commit is contained in:
@@ -43,19 +43,32 @@ impl fmt::Display for HeaderMagicBytes {
|
||||
|
||||
pub const DATA_START_OFFSET: usize = 0x400;
|
||||
|
||||
pub enum Cipher {
|
||||
pub enum Decipher {
|
||||
V1(CipherV1),
|
||||
V2(CipherV2),
|
||||
}
|
||||
|
||||
impl Cipher {
|
||||
impl Decipher {
|
||||
pub fn new<T: AsRef<[u8]>>(header: &Header, ekey: Option<T>) -> Result<Decipher> {
|
||||
let cipher = match header.version {
|
||||
1 => Decipher::V1(CipherV1::new(header.resource_id)),
|
||||
2 => match ekey {
|
||||
Some(ekey) => Decipher::V2(CipherV2::new_from_ekey(ekey)?),
|
||||
None => Err(KuwoCryptoError::V2EKeyRequired)?,
|
||||
},
|
||||
version => Err(KuwoCryptoError::UnsupportedVersion(version as usize))?,
|
||||
};
|
||||
|
||||
Ok(cipher)
|
||||
}
|
||||
|
||||
pub fn decrypt<T>(&self, data: &mut T, offset: usize)
|
||||
where
|
||||
T: AsMut<[u8]> + ?Sized,
|
||||
{
|
||||
match self {
|
||||
Cipher::V1(cipher) => cipher.decrypt(data, offset),
|
||||
Cipher::V2(cipher) => cipher.decrypt(data, offset),
|
||||
Decipher::V1(cipher) => cipher.decrypt(data, offset),
|
||||
Decipher::V2(cipher) => cipher.decrypt(data, offset),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,22 +118,6 @@ impl Header {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_decipher<T>(&self, ekey: Option<T>) -> Result<Cipher>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
let cipher = match self.version {
|
||||
1 => Cipher::V1(CipherV1::new(self.resource_id)),
|
||||
2 => match ekey {
|
||||
Some(ekey) => Cipher::V2(CipherV2::new_from_ekey(ekey)?),
|
||||
None => Err(KuwoCryptoError::V2EKeyRequired)?,
|
||||
},
|
||||
version => Err(KuwoCryptoError::UnsupportedVersion(version as usize))?,
|
||||
};
|
||||
|
||||
Ok(cipher)
|
||||
}
|
||||
|
||||
/// Get the quality id
|
||||
/// Used for matching Android MMKV id.
|
||||
pub fn get_quality_id(&self) -> u32 {
|
||||
|
||||
Reference in New Issue
Block a user