From e92dc089647c154501e4f09a274986dedc2b32e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Fri, 6 Sep 2024 00:51:08 +0100 Subject: [PATCH] refactor: move base64 to shared utils package --- .idea/lib_um_crypto.iml | 1 + Cargo.lock | 106 +++++++++++++++++++++++++++++++++- um_crypto/kuwo/Cargo.toml | 4 +- um_crypto/kuwo/src/des/mod.rs | 14 +---- um_crypto/utils/Cargo.toml | 7 +++ um_crypto/utils/src/base64.rs | 22 +++++++ um_crypto/utils/src/lib.rs | 1 + 7 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 um_crypto/utils/Cargo.toml create mode 100644 um_crypto/utils/src/base64.rs create mode 100644 um_crypto/utils/src/lib.rs diff --git a/.idea/lib_um_crypto.iml b/.idea/lib_um_crypto.iml index b1d8e6c..2a9d528 100644 --- a/.idea/lib_um_crypto.iml +++ b/.idea/lib_um_crypto.iml @@ -7,6 +7,7 @@ + diff --git a/Cargo.lock b/Cargo.lock index 06fb395..f39033c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,17 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -182,6 +193,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + [[package]] name = "log" version = "0.4.22" @@ -204,6 +221,15 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -222,6 +248,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "same-file" version = "1.0.6" @@ -260,6 +316,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tc_tea" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab26285e70ee5cbec8582c76b8124dfe65b564b43dad14b9caca6fd127d66d2" +dependencies = [ + "rand", + "rand_chacha", +] + [[package]] name = "thiserror" version = "1.0.63" @@ -288,6 +354,7 @@ dependencies = [ "clap", "umc_kuwo", "umc_qmc", + "umc_utils", ] [[package]] @@ -307,9 +374,9 @@ name = "umc_kuwo" version = "0.1.0" dependencies = [ "anyhow", - "base64", "itertools", "thiserror", + "umc_utils", ] [[package]] @@ -317,10 +384,18 @@ name = "umc_qmc" version = "0.1.0" dependencies = [ "anyhow", - "base64", "byteorder", "itertools", + "tc_tea", "thiserror", + "umc_utils", +] + +[[package]] +name = "umc_utils" +version = "0.1.0" +dependencies = [ + "base64", ] [[package]] @@ -345,6 +420,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.93" @@ -538,3 +619,24 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/um_crypto/kuwo/Cargo.toml b/um_crypto/kuwo/Cargo.toml index 24ec27d..680d3a0 100644 --- a/um_crypto/kuwo/Cargo.toml +++ b/um_crypto/kuwo/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -base64 = "0.22.1" -itertools = "0.13.0" anyhow = "1.0.86" +itertools = "0.13.0" thiserror = "1.0.63" +umc_utils = { path = "../utils" } diff --git a/um_crypto/kuwo/src/des/mod.rs b/um_crypto/kuwo/src/des/mod.rs index be903db..998fb0d 100644 --- a/um_crypto/kuwo/src/des/mod.rs +++ b/um_crypto/kuwo/src/des/mod.rs @@ -1,22 +1,14 @@ use anyhow::Result; -use base64::alphabet; -use base64::engine::{DecodePaddingMode, GeneralPurpose as Base64Engine, GeneralPurposeConfig}; -use base64::prelude::*; mod constants; mod core; mod helper; use core::{KuwoDes, Mode}; - -/// Don't add padding when encoding, and require no padding when decoding. -const B64: Base64Engine = Base64Engine::new( - &alphabet::STANDARD, - GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent), -); +use umc_utils::base64; /// Decrypt string content pub fn decrypt_ksing(data: &str, key: &[u8; 8]) -> Result { - let mut decoded = B64.decode(data)?; + let mut decoded = base64::decode(data)?; let des = KuwoDes::new(key, Mode::Decrypt); des.transform(&mut decoded[..])?; @@ -35,7 +27,7 @@ pub fn encrypt_ksing>(data: T, key: &[u8; 8]) -> Result { let des = KuwoDes::new(key, Mode::Encrypt); des.transform(&mut data[..])?; - Ok(B64.encode(data)) + Ok(base64::encode(data)) } pub fn decode_ekey(data: &str, key: &[u8; 8]) -> Result { diff --git a/um_crypto/utils/Cargo.toml b/um_crypto/utils/Cargo.toml new file mode 100644 index 0000000..71b9d4a --- /dev/null +++ b/um_crypto/utils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "umc_utils" +version = "0.1.0" +edition = "2021" + +[dependencies] +base64 = "0.22.1" diff --git a/um_crypto/utils/src/base64.rs b/um_crypto/utils/src/base64.rs new file mode 100644 index 0000000..5d38f52 --- /dev/null +++ b/um_crypto/utils/src/base64.rs @@ -0,0 +1,22 @@ +use base64::engine::{DecodePaddingMode, GeneralPurpose as Base64Engine, GeneralPurposeConfig}; +use base64::{alphabet, DecodeError, Engine}; + +/// Don't add padding when encoding, and require no padding when decoding. +pub const ENGINE: Base64Engine = Base64Engine::new( + &alphabet::STANDARD, + GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent), +); + +pub fn encode(data: T) -> String +where + T: AsRef<[u8]>, +{ + ENGINE.encode(data) +} + +pub fn decode(data: T) -> Result, DecodeError> +where + T: AsRef<[u8]>, +{ + ENGINE.decode(data) +} diff --git a/um_crypto/utils/src/lib.rs b/um_crypto/utils/src/lib.rs new file mode 100644 index 0000000..39abdce --- /dev/null +++ b/um_crypto/utils/src/lib.rs @@ -0,0 +1 @@ +pub mod base64;