[kwm] refactor: move Decipher new fn under struct
This commit is contained in:
parent
e45d09cf8e
commit
75ebe1e631
@ -43,19 +43,32 @@ impl fmt::Display for HeaderMagicBytes {
|
|||||||
|
|
||||||
pub const DATA_START_OFFSET: usize = 0x400;
|
pub const DATA_START_OFFSET: usize = 0x400;
|
||||||
|
|
||||||
pub enum Cipher {
|
pub enum Decipher {
|
||||||
V1(CipherV1),
|
V1(CipherV1),
|
||||||
V2(CipherV2),
|
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)
|
pub fn decrypt<T>(&self, data: &mut T, offset: usize)
|
||||||
where
|
where
|
||||||
T: AsMut<[u8]> + ?Sized,
|
T: AsMut<[u8]> + ?Sized,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Cipher::V1(cipher) => cipher.decrypt(data, offset),
|
Decipher::V1(cipher) => cipher.decrypt(data, offset),
|
||||||
Cipher::V2(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
|
/// Get the quality id
|
||||||
/// Used for matching Android MMKV id.
|
/// Used for matching Android MMKV id.
|
||||||
pub fn get_quality_id(&self) -> u32 {
|
pub fn get_quality_id(&self) -> u32 {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::errors::map_js_error;
|
use crate::errors::map_js_error;
|
||||||
use crate::exports::qmc::JsQMC2;
|
use crate::exports::qmc::JsQMC2;
|
||||||
use umc_kuwo::kwm_v1::CipherV1;
|
use umc_kuwo::kwm_v1::CipherV1;
|
||||||
use umc_kuwo::{Cipher, Header};
|
use umc_kuwo::{Decipher, Header};
|
||||||
use wasm_bindgen::prelude::wasm_bindgen;
|
use wasm_bindgen::prelude::wasm_bindgen;
|
||||||
use wasm_bindgen::JsError;
|
use wasm_bindgen::JsError;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ impl JsKuwoHeader {
|
|||||||
/// Create an instance of cipher (decipher) for decryption
|
/// Create an instance of cipher (decipher) for decryption
|
||||||
#[wasm_bindgen(js_name=makeDecipher)]
|
#[wasm_bindgen(js_name=makeDecipher)]
|
||||||
pub fn make_decipher(&self, ekey: Option<String>) -> Result<JsCipher, JsError> {
|
pub fn make_decipher(&self, ekey: Option<String>) -> Result<JsCipher, JsError> {
|
||||||
let cipher = self.0.get_decipher(ekey).map_err(map_js_error)?;
|
let cipher = Decipher::new(&self.0, ekey).map_err(map_js_error)?;
|
||||||
Ok(JsCipher(cipher))
|
Ok(JsCipher(cipher))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ pub fn js_kuwo_v2_cipher_factory(ekey: &str) -> Result<JsQMC2, JsError> {
|
|||||||
|
|
||||||
/// Common V1/V2 wrapper interface, derived from `KuwoHeader.makeCipher`
|
/// Common V1/V2 wrapper interface, derived from `KuwoHeader.makeCipher`
|
||||||
#[wasm_bindgen(js_name=KWMCipher)]
|
#[wasm_bindgen(js_name=KWMCipher)]
|
||||||
pub struct JsCipher(Cipher);
|
pub struct JsCipher(Decipher);
|
||||||
|
|
||||||
#[wasm_bindgen(js_class=KWMCipher)]
|
#[wasm_bindgen(js_class=KWMCipher)]
|
||||||
impl JsCipher {
|
impl JsCipher {
|
||||||
|
Loading…
Reference in New Issue
Block a user