From 91f54423a27c5d54a388db22466256e0ebdae809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sun, 4 May 2025 21:15:31 +0900 Subject: [PATCH] refactor: remove anyhow from kuwo --- um_crypto/kuwo/Cargo.toml | 1 - um_crypto/kuwo/src/des/core.rs | 5 ++--- um_crypto/kuwo/src/des/mod.rs | 16 +++++++++------- um_crypto/kuwo/src/lib.rs | 25 ++++++++++++++++++------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/um_crypto/kuwo/Cargo.toml b/um_crypto/kuwo/Cargo.toml index d0974c5..3324c48 100644 --- a/um_crypto/kuwo/Cargo.toml +++ b/um_crypto/kuwo/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.9" edition = "2021" [dependencies] -anyhow = "1.0.86" byteorder = "1.5.0" itertools = "0.13.0" thiserror = "2.0.7" diff --git a/um_crypto/kuwo/src/des/core.rs b/um_crypto/kuwo/src/des/core.rs index 54cc58a..d0a20d2 100644 --- a/um_crypto/kuwo/src/des/core.rs +++ b/um_crypto/kuwo/src/des/core.rs @@ -1,6 +1,5 @@ use super::{constants, helper}; use crate::KuwoCryptoError; -use anyhow::Result; use itertools::Either; /// Encrypt or Decrypt? @@ -82,13 +81,13 @@ impl KuwoDes { state } - pub fn transform(&self, data: &mut [u8]) -> Result<()> { + pub fn transform(&self, data: &mut [u8]) -> Result<(), KuwoCryptoError> { if data.len() % 8 != 0 { Err(KuwoCryptoError::InvalidDesDataSize(data.len()))? } for block in data.chunks_exact_mut(8) { - let value = u64::from_le_bytes(block.try_into()?); + let value = u64::from_le_bytes(block.try_into().unwrap_or_default()); let value = self.transform_block(value); block.copy_from_slice(&value.to_le_bytes()); } diff --git a/um_crypto/kuwo/src/des/mod.rs b/um_crypto/kuwo/src/des/mod.rs index ad49e84..25837be 100644 --- a/um_crypto/kuwo/src/des/mod.rs +++ b/um_crypto/kuwo/src/des/mod.rs @@ -1,13 +1,13 @@ -use anyhow::Result; - mod constants; mod core; mod helper; use core::{KuwoDes, Mode}; use umc_utils::base64; +use crate::KuwoCryptoError; + /// Decrypt string content -pub fn decrypt_ksing>(data: T, key: &[u8; 8]) -> Result { +pub fn decrypt_ksing>(data: T, key: &[u8; 8]) -> Result { let mut decoded = base64::decode(data)?; let des = KuwoDes::new(key, Mode::Decrypt); @@ -20,17 +20,19 @@ pub fn decrypt_ksing>(data: T, key: &[u8; 8]) -> Result { Ok(result) } -pub fn encrypt_ksing>(data: T, key: &[u8; 8]) -> Result { +pub fn encrypt_ksing>(data: T, key: &[u8; 8]) -> Result { let mut data = Vec::from(data.as_ref()); - let padded_len = ((data.len() + 7) / 8) * 8; - data.resize(padded_len, 0u8); + let padded_len = data.len() % 8; + if padded_len != 0 { + data.resize(data.len() + (8 - padded_len), 0); + } let des = KuwoDes::new(key, Mode::Encrypt); des.transform(&mut data[..])?; Ok(base64::encode(data)) } -pub fn decode_ekey>(data: T, key: &[u8; 8]) -> Result { +pub fn decode_ekey>(data: T, key: &[u8; 8]) -> Result { let decoded = decrypt_ksing(data, key)?; Ok(decoded[16..].to_string()) } diff --git a/um_crypto/kuwo/src/lib.rs b/um_crypto/kuwo/src/lib.rs index 4a1e3d2..2560184 100644 --- a/um_crypto/kuwo/src/lib.rs +++ b/um_crypto/kuwo/src/lib.rs @@ -1,15 +1,14 @@ use crate::kwm_v1::CipherV1; -use anyhow::Result; use byteorder::{ReadBytesExt, LE}; use std::fmt; use std::io::{Cursor, Read}; use thiserror::Error; -use umc_qmc::QMCv2Cipher; +use umc_utils::base64::DecodeError; pub mod des; pub mod kwm_v1; -pub use umc_qmc::QMCv2Cipher as CipherV2; +pub use umc_qmc::{QMCv2Cipher as CipherV2, QmcCryptoError}; /// Commonly used secret key for Kuwo services. pub const SECRET_KEY: [u8; 8] = *b"ylzsxkwm"; @@ -30,6 +29,15 @@ pub enum KuwoCryptoError { #[error("KWM: Unsupported version {0}")] UnsupportedVersion(usize), + + #[error("Failed to decode base64: {0}")] + Base64DecodeError(#[from] DecodeError), + + #[error("{0}")] + QMCCipherError(#[from] QmcCryptoError), + + #[error("Failed to read data: {0}")] + IoError(#[from] std::io::Error), } impl fmt::Display for HeaderMagicBytes { @@ -49,7 +57,10 @@ pub enum Decipher { } impl Decipher { - pub fn new>(header: &Header, ekey: Option) -> Result { + pub fn new>( + header: &Header, + ekey: Option, + ) -> Result { let cipher = match header.version { 1 => Decipher::V1(CipherV1::new(header.resource_id)), 2 => match ekey { @@ -89,7 +100,7 @@ impl Header { const MAGIC_1: [u8; 16] = *b"yeelion-kuwo-tme"; const MAGIC_2: [u8; 16] = *b"yeelion-kuwo\0\0\0\0"; - pub fn from_bytes(bytes: T) -> Result + pub fn from_bytes(bytes: T) -> Result where T: AsRef<[u8]>, { @@ -128,10 +139,10 @@ impl Header { } } -pub struct CipherBoDian(QMCv2Cipher); +pub struct CipherBoDian(CipherV2); impl CipherBoDian { - pub fn new>(ekey: T) -> Result { + pub fn new>(ekey: T) -> Result { let ekey = des::decode_ekey(ekey, &SECRET_KEY)?; let cipher = CipherV2::new_from_ekey(ekey.as_str())?; Ok(Self(cipher))