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

@@ -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> {