refactor: rename des_impl.rs

This commit is contained in:
鲁树人 2024-09-02 21:11:17 +01:00
parent d52819b4b3
commit 0806a97fb2
3 changed files with 124 additions and 119 deletions

View File

@ -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) ((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)) (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 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 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 { if value == 255 {
return 0; return 0;
} }
@ -35,7 +35,7 @@ fn test_get_u64_by_shift_idx() {
assert_eq!(get_u64_by_shift_idx(63), 0x8000000000000000); 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)| { table.iter().enumerate().fold(0u64, |acc, (i, &idx)| {
match get_u64_by_shift_idx(idx) & src_value { match get_u64_by_shift_idx(idx) & src_value {
0 => acc, 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 map_u64(src_value as u64, table) as u32
} }

View File

@ -4,9 +4,9 @@ use base64::engine::{DecodePaddingMode, GeneralPurpose as Base64Engine, GeneralP
use base64::prelude::*; use base64::prelude::*;
mod constants; mod constants;
mod des_impl; mod core;
mod helper; mod helper;
pub use des_impl::{KuwoDes, Mode}; use core::{KuwoDes, Mode};
/// Don't add padding when encoding, and require no padding when decoding. /// Don't add padding when encoding, and require no padding when decoding.
const B64: Base64Engine = Base64Engine::new( const B64: Base64Engine = Base64Engine::new(
@ -38,12 +38,17 @@ pub fn encrypt_ksing<T: AsRef<[u8]>>(data: T, key: &[u8; 8]) -> Result<String> {
Ok(B64.encode(data)) Ok(B64.encode(data))
} }
pub fn decode_ekey<T: AsRef<[u8]>>(data: &str, key: &[u8; 8]) -> Result<String> {
let decoded = decrypt_ksing(data, key)?;
Ok(decoded[16..].to_string())
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
#[test] #[test]
fn test_decode() { fn test_ksing_decode() {
let expected = "hello world"; let expected = "hello world";
let decoded = let decoded =
decrypt_ksing("tx5ct5ilzeLs7pN1C4RI6w==", b"12345678").expect("decrypt failed"); decrypt_ksing("tx5ct5ilzeLs7pN1C4RI6w==", b"12345678").expect("decrypt failed");