Compare commits

...

3 Commits

6 changed files with 59 additions and 16 deletions

1
Cargo.lock generated
View File

@ -485,6 +485,7 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"getrandom", "getrandom",
"um_audio", "um_audio",
"umc_kgm",
"umc_kuwo", "umc_kuwo",
"umc_ncm", "umc_ncm",
"umc_qmc", "umc_qmc",

View File

@ -23,6 +23,7 @@ getrandom = { version = "0.2", features = ["js"] }
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying. # code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true }
umc_kgm = { path = "../um_crypto/kgm" }
umc_kuwo = { path = "../um_crypto/kuwo" } umc_kuwo = { path = "../um_crypto/kuwo" }
umc_ncm = { path = "../um_crypto/ncm" } umc_ncm = { path = "../um_crypto/ncm" }
umc_qmc = { path = "../um_crypto/qmc" } umc_qmc = { path = "../um_crypto/qmc" }

View File

@ -0,0 +1,37 @@
use umc_kgm::{header::Header, Decipher};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsError;
/// KuGou KGM file header.
#[wasm_bindgen(js_name=KuGouHeader)]
pub struct JsKuGouHeader(Header);
#[wasm_bindgen(js_class = KuGouHeader)]
impl JsKuGouHeader {
/// Parse the KuGou header (0x400 bytes)
pub fn parse(header: &[u8]) -> Result<JsKuGouHeader, JsError> {
Ok(JsKuGouHeader(
Header::from_buffer(header).map_err(JsError::from)?,
))
}
}
/// KuGou KGM file decipher.
#[wasm_bindgen(js_name=KuGouDecipher)]
pub struct JsKuGouDecipher(Decipher);
#[wasm_bindgen(js_class=KuGouDecipher)]
impl JsKuGouDecipher {
/// Create an instance of cipher (decipher) for decryption
#[wasm_bindgen(constructor)]
pub fn new(header: &JsKuGouHeader) -> Result<JsKuGouDecipher, JsError> {
Ok(JsKuGouDecipher(
Decipher::new(&header.0).map_err(JsError::from)?,
))
}
/// Decrypt a buffer.
pub fn decrypt(&self, buffer: &mut [u8], offset: usize) {
self.0.decrypt(buffer, offset)
}
}

View File

@ -23,13 +23,6 @@ impl JsKuwoHeader {
pub fn quality_id(&self) -> u32 { pub fn quality_id(&self) -> u32 {
self.0.get_quality_id() self.0.get_quality_id()
} }
/// Create an instance of cipher (decipher) for decryption
#[wasm_bindgen(js_name=makeDecipher)]
pub fn make_decipher(&self, ekey: Option<String>) -> Result<JsCipher, JsError> {
let cipher = Decipher::new(&self.0, ekey).map_err(map_js_error)?;
Ok(JsCipher(cipher))
}
} }
/// Create a decipher instance for "BoDian Music". /// Create a decipher instance for "BoDian Music".
@ -40,11 +33,11 @@ pub fn js_kuwo_bodian_cipher_factory(ekey: &str) -> Result<JsQMC2, JsError> {
} }
/// Kuwo KWM v1 /// Kuwo KWM v1
#[wasm_bindgen(js_name=KWMCipherV1)] #[wasm_bindgen(js_name=KWMDecipherV1)]
pub struct JsCipherV1(CipherV1); pub struct JsDecipherV1(CipherV1);
#[wasm_bindgen(js_class=KWMCipherV1)] #[wasm_bindgen(js_class=KWMDecipherV1)]
impl JsCipherV1 { impl JsDecipherV1 {
/// Create a decipher instance for "Kuwo KWM v1". /// Create a decipher instance for "Kuwo KWM v1".
pub fn decrypt(&self, buffer: &mut [u8], offset: usize) { pub fn decrypt(&self, buffer: &mut [u8], offset: usize) {
self.0.decrypt(buffer, offset) self.0.decrypt(buffer, offset)
@ -58,11 +51,21 @@ 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=KWMDecipher)]
pub struct JsCipher(Decipher); pub struct JsDecipher(Decipher);
#[wasm_bindgen(js_class=KWMDecipher)]
impl JsDecipher {
/// Create an instance of cipher (decipher) for decryption
#[wasm_bindgen(constructor)]
pub fn make_decipher(
header: &JsKuwoHeader,
ekey: Option<String>,
) -> Result<JsDecipher, JsError> {
let cipher = Decipher::new(&header.0, ekey).map_err(map_js_error)?;
Ok(JsDecipher(cipher))
}
#[wasm_bindgen(js_class=KWMCipher)]
impl JsCipher {
/// Decrypt buffer at given offset. /// Decrypt buffer at given offset.
pub fn decrypt(&self, buffer: &mut [u8], offset: usize) { pub fn decrypt(&self, buffer: &mut [u8], offset: usize) {
self.0.decrypt(buffer, offset) self.0.decrypt(buffer, offset)

View File

@ -1,4 +1,5 @@
pub mod audio; pub mod audio;
pub mod kgm;
pub mod kuwo; pub mod kuwo;
pub mod ncm; pub mod ncm;
pub mod qmc; pub mod qmc;

View File

@ -1,6 +1,6 @@
{ {
"name": "@unlock-music/crypto", "name": "@unlock-music/crypto",
"version": "0.0.0-alpha.11", "version": "0.0.0-alpha.12",
"description": "Project Unlock Music: 加解密支持库", "description": "Project Unlock Music: 加解密支持库",
"scripts": { "scripts": {
"build": "node build.js", "build": "node build.js",