mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-08 04:29:54 +00:00
refactor: move base64 to shared utils package
This commit is contained in:
@@ -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" }
|
||||
|
||||
@@ -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> {
|
||||
|
||||
7
um_crypto/utils/Cargo.toml
Normal file
7
um_crypto/utils/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "umc_utils"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
22
um_crypto/utils/src/base64.rs
Normal file
22
um_crypto/utils/src/base64.rs
Normal 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)
|
||||
}
|
||||
1
um_crypto/utils/src/lib.rs
Normal file
1
um_crypto/utils/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod base64;
|
||||
Reference in New Issue
Block a user