refactor: move base64 to shared utils package

This commit is contained in:
鲁树人 2024-09-06 00:51:08 +01:00
parent bfa66c6e39
commit e92dc08964
7 changed files with 140 additions and 15 deletions

View File

@ -7,6 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/um_crypto/kuwo/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/um_crypto/qmc/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/um_cli/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/um_crypto/utils/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

106
Cargo.lock generated
View File

@ -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",
]

View File

@ -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" }

View File

@ -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<String> {
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<T: AsRef<[u8]>>(data: T, key: &[u8; 8]) -> Result<String> {
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<String> {

View File

@ -0,0 +1,7 @@
[package]
name = "umc_utils"
version = "0.1.0"
edition = "2021"
[dependencies]
base64 = "0.22.1"

View File

@ -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<T>(data: T) -> String
where
T: AsRef<[u8]>,
{
ENGINE.encode(data)
}
pub fn decode<T>(data: T) -> Result<Vec<u8>, DecodeError>
where
T: AsRef<[u8]>,
{
ENGINE.decode(data)
}

View File

@ -0,0 +1 @@
pub mod base64;