From d51cfae9c24b794bc14867ae8df390c6b14cc40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Mon, 2 Sep 2024 21:11:17 +0100 Subject: [PATCH] refactor: rename `des_impl.rs` --- um_crypto/kuwo/src/des/{des_impl.rs => core.rs} | 0 um_crypto/kuwo/src/des/helper.rs | 14 +++++++------- um_crypto/kuwo/src/des/mod.rs | 11 ++++++++--- 3 files changed, 15 insertions(+), 10 deletions(-) rename um_crypto/kuwo/src/des/{des_impl.rs => core.rs} (100%) diff --git a/um_crypto/kuwo/src/des/des_impl.rs b/um_crypto/kuwo/src/des/core.rs similarity index 100% rename from um_crypto/kuwo/src/des/des_impl.rs rename to um_crypto/kuwo/src/des/core.rs diff --git a/um_crypto/kuwo/src/des/helper.rs b/um_crypto/kuwo/src/des/helper.rs index 09af078..1d78999 100644 --- a/um_crypto/kuwo/src/des/helper.rs +++ b/um_crypto/kuwo/src/des/helper.rs @@ -1,20 +1,20 @@ -pub(super) const fn make_u64(hi32: u32, lo32: u32) -> u64 { +pub const fn make_u64(hi32: u32, lo32: u32) -> u64 { ((hi32 as u64) << 32) | (lo32 as u64) } -pub(super) const fn swap_u64_side(value: u64) -> u64 { +pub const fn swap_u64_side(value: u64) -> u64 { (value.wrapping_shr(32)) | (value.wrapping_shl(32)) } -pub(super) const fn u64_get_lo32(value: u64) -> u32 { +pub const fn u64_get_lo32(value: u64) -> u32 { value as u32 } -pub(super) const fn u64_get_hi32(value: u64) -> u32 { +pub const fn u64_get_hi32(value: u64) -> u32 { value.wrapping_shr(32) as u32 } -pub(super) fn get_u64_by_shift_idx(value: u8) -> u64 { +pub fn get_u64_by_shift_idx(value: u8) -> u64 { if value == 255 { return 0; } @@ -35,7 +35,7 @@ fn test_get_u64_by_shift_idx() { assert_eq!(get_u64_by_shift_idx(63), 0x8000000000000000); } -pub(super) fn map_u64(src_value: u64, table: &[u8]) -> u64 { +pub fn map_u64(src_value: u64, table: &[u8]) -> u64 { table.iter().enumerate().fold(0u64, |acc, (i, &idx)| { match get_u64_by_shift_idx(idx) & src_value { 0 => acc, @@ -44,6 +44,6 @@ pub(super) fn map_u64(src_value: u64, table: &[u8]) -> u64 { }) } -pub(super) fn map_u32(src_value: u32, table: &[u8]) -> u32 { +pub fn map_u32(src_value: u32, table: &[u8]) -> u32 { map_u64(src_value as u64, table) as u32 } diff --git a/um_crypto/kuwo/src/des/mod.rs b/um_crypto/kuwo/src/des/mod.rs index d7be8f2..893edc2 100644 --- a/um_crypto/kuwo/src/des/mod.rs +++ b/um_crypto/kuwo/src/des/mod.rs @@ -4,9 +4,9 @@ use base64::engine::{DecodePaddingMode, GeneralPurpose as Base64Engine, GeneralP use base64::prelude::*; mod constants; -mod des_impl; +mod core; mod helper; -pub use des_impl::{KuwoDes, Mode}; +use core::{KuwoDes, Mode}; /// Don't add padding when encoding, and require no padding when decoding. const B64: Base64Engine = Base64Engine::new( @@ -38,12 +38,17 @@ pub fn encrypt_ksing>(data: T, key: &[u8; 8]) -> Result { Ok(B64.encode(data)) } +pub fn decode_ekey>(data: &str, key: &[u8; 8]) -> Result { + let decoded = decrypt_ksing(data, key)?; + Ok(decoded[16..].to_string()) +} + #[cfg(test)] mod test { use super::*; #[test] - fn test_decode() { + fn test_ksing_decode() { let expected = "hello world"; let decoded = decrypt_ksing("tx5ct5ilzeLs7pN1C4RI6w==", b"12345678").expect("decrypt failed");