refactor: move base64 to shared utils package

This commit is contained in:
鲁树人 2024-09-06 00:51:08 +01:00
parent fdc9c5d138
commit b643b53913
9 changed files with 343 additions and 218 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,108 +1,108 @@
pub const KEY_RND_SHIFTS: [u8; 16] = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
pub const KEY_SHIFT_MASKS: [u64; 3] = [0, 0x100001, 0x300003];
pub const KEY_SHIFT_LEFT_MASKS: [u64; 16] = {
let mut result = [0u64; 16];
let mut i = 0usize;
while i < 16 {
result[i] = KEY_SHIFT_MASKS[KEY_RND_SHIFTS[i] as usize];
i += 1;
}
result
};
pub const SBOXES: [[u8; 64]; 8] = [
[
13, 7, 10, 0, 6, 9, 5, 15, 8, 4, 3, 10, 11, 14, 12, 5, 2, 11, 9, 6, 15, 12, 0, 3, 4, 1, 14,
13, 1, 2, 7, 8, 1, 2, 12, 15, 10, 4, 0, 3, 13, 14, 6, 9, 7, 8, 9, 6, 15, 1, 5, 12, 3, 10,
14, 5, 8, 7, 11, 0, 4, 13, 2, 11,
],
[
4, 1, 3, 10, 15, 12, 5, 0, 2, 11, 9, 6, 8, 7, 6, 9, 11, 4, 12, 15, 0, 3, 10, 5, 14, 13, 7,
8, 13, 14, 1, 2, 13, 6, 14, 9, 4, 1, 2, 14, 11, 13, 5, 0, 1, 10, 8, 3, 0, 11, 3, 5, 9, 4,
15, 2, 7, 8, 12, 15, 10, 7, 6, 12,
],
[
12, 9, 0, 7, 9, 2, 14, 1, 10, 15, 3, 4, 6, 12, 5, 11, 1, 14, 13, 0, 2, 8, 7, 13, 15, 5, 4,
10, 8, 3, 11, 6, 10, 4, 6, 11, 7, 9, 0, 6, 4, 2, 13, 1, 9, 15, 3, 8, 15, 3, 1, 14, 12, 5,
11, 0, 2, 12, 14, 7, 5, 10, 8, 13,
],
[
2, 4, 8, 15, 7, 10, 13, 6, 4, 1, 3, 12, 11, 7, 14, 0, 12, 2, 5, 9, 10, 13, 0, 3, 1, 11, 15,
5, 6, 8, 9, 14, 14, 11, 5, 6, 4, 1, 3, 10, 2, 12, 15, 0, 13, 2, 8, 5, 11, 8, 0, 15, 7, 14,
9, 4, 12, 7, 10, 9, 1, 13, 6, 3,
],
[
7, 10, 1, 15, 0, 12, 11, 5, 14, 9, 8, 3, 9, 7, 4, 8, 13, 6, 2, 1, 6, 11, 12, 2, 3, 0, 5,
14, 10, 13, 15, 4, 13, 3, 4, 9, 6, 10, 1, 12, 11, 0, 2, 5, 0, 13, 14, 2, 8, 15, 7, 4, 15,
1, 10, 7, 5, 6, 12, 11, 3, 8, 9, 14,
],
[
10, 13, 1, 11, 6, 8, 11, 5, 9, 4, 12, 2, 15, 3, 2, 14, 0, 6, 13, 1, 3, 15, 4, 10, 14, 9, 7,
12, 5, 0, 8, 7, 13, 1, 2, 4, 3, 6, 12, 11, 0, 13, 5, 14, 6, 8, 15, 2, 7, 10, 8, 15, 4, 9,
11, 5, 9, 0, 14, 3, 10, 7, 1, 12,
],
[
15, 0, 9, 5, 6, 10, 12, 9, 8, 7, 2, 12, 3, 13, 5, 2, 1, 14, 7, 8, 11, 4, 0, 3, 14, 11, 13,
6, 4, 1, 10, 15, 3, 13, 12, 11, 15, 3, 6, 0, 4, 10, 1, 7, 8, 4, 11, 14, 13, 8, 0, 6, 2, 15,
9, 5, 7, 1, 10, 12, 14, 2, 5, 9,
],
[
14, 4, 3, 15, 2, 13, 5, 3, 13, 14, 6, 9, 11, 2, 0, 5, 4, 1, 10, 12, 15, 6, 9, 10, 1, 8, 12,
7, 8, 11, 7, 0, 0, 15, 10, 5, 14, 4, 9, 10, 7, 8, 12, 3, 13, 1, 3, 6, 15, 12, 6, 11, 2, 9,
5, 0, 4, 2, 11, 14, 1, 7, 8, 13,
],
];
// custom
pub const PBOX: [u8; 32] = [
15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9, 1, 7, 23, 13, 31, 26, 2, 8, 18, 12,
29, 5, 21, 10, 3, 24,
];
// custom
pub const IP: [u8; 64] = [
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63,
55, 47, 39, 31, 23, 15, 7, 56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52,
44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6,
];
// custom
pub const IP_INV: [u8; 64] = [
39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24,
];
// custom
pub const KEY_PERMUTATION_TABLE: [u8; 56] = [
//key_param_c + key_param_d
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59,
51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3,
];
// custom
pub const KEY_COMPRESSION: [u8; 64] = [
13, 16, 10, 23, 0, 4, 255, 255, 2, 27, 14, 5, 20, 9, 255, 255, 22, 18, 11, 3, 25, 7, 255, 255,
15, 6, 26, 19, 12, 1, 255, 255, 40, 51, 30, 36, 46, 54, 255, 255, 29, 39, 50, 44, 32, 47, 255,
255, 43, 48, 38, 55, 33, 52, 255, 255, 45, 41, 49, 35, 28, 31, 255, 255,
];
// custom
pub const KEY_EXPANSION: [u8; 64] = [
31, 0, 1, 2, 3, 4, 255, 255, 3, 4, 5, 6, 7, 8, 255, 255, 7, 8, 9, 10, 11, 12, 255, 255, 11, 12,
13, 14, 15, 16, 255, 255, 15, 16, 17, 18, 19, 20, 255, 255, 19, 20, 21, 22, 23, 24, 255, 255,
23, 24, 25, 26, 27, 28, 255, 255, 27, 28, 29, 30, 31, 30, 255, 255,
];
pub const U64_SHIFT_TABLE_CACHE: [u64; 64] = {
let mut data = [0u64; 64];
let mut i = 0;
while i < 32 {
data[i] = 1u64 << i;
data[i + 32] = 1u64 << (i + 32);
i += 1;
}
data
};
pub const KEY_RND_SHIFTS: [u8; 16] = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
pub const KEY_SHIFT_MASKS: [u64; 3] = [0, 0x100001, 0x300003];
pub const KEY_SHIFT_LEFT_MASKS: [u64; 16] = {
let mut result = [0u64; 16];
let mut i = 0usize;
while i < 16 {
result[i] = KEY_SHIFT_MASKS[KEY_RND_SHIFTS[i] as usize];
i += 1;
}
result
};
pub const SBOXES: [[u8; 64]; 8] = [
[
13, 7, 10, 0, 6, 9, 5, 15, 8, 4, 3, 10, 11, 14, 12, 5, 2, 11, 9, 6, 15, 12, 0, 3, 4, 1, 14,
13, 1, 2, 7, 8, 1, 2, 12, 15, 10, 4, 0, 3, 13, 14, 6, 9, 7, 8, 9, 6, 15, 1, 5, 12, 3, 10,
14, 5, 8, 7, 11, 0, 4, 13, 2, 11,
],
[
4, 1, 3, 10, 15, 12, 5, 0, 2, 11, 9, 6, 8, 7, 6, 9, 11, 4, 12, 15, 0, 3, 10, 5, 14, 13, 7,
8, 13, 14, 1, 2, 13, 6, 14, 9, 4, 1, 2, 14, 11, 13, 5, 0, 1, 10, 8, 3, 0, 11, 3, 5, 9, 4,
15, 2, 7, 8, 12, 15, 10, 7, 6, 12,
],
[
12, 9, 0, 7, 9, 2, 14, 1, 10, 15, 3, 4, 6, 12, 5, 11, 1, 14, 13, 0, 2, 8, 7, 13, 15, 5, 4,
10, 8, 3, 11, 6, 10, 4, 6, 11, 7, 9, 0, 6, 4, 2, 13, 1, 9, 15, 3, 8, 15, 3, 1, 14, 12, 5,
11, 0, 2, 12, 14, 7, 5, 10, 8, 13,
],
[
2, 4, 8, 15, 7, 10, 13, 6, 4, 1, 3, 12, 11, 7, 14, 0, 12, 2, 5, 9, 10, 13, 0, 3, 1, 11, 15,
5, 6, 8, 9, 14, 14, 11, 5, 6, 4, 1, 3, 10, 2, 12, 15, 0, 13, 2, 8, 5, 11, 8, 0, 15, 7, 14,
9, 4, 12, 7, 10, 9, 1, 13, 6, 3,
],
[
7, 10, 1, 15, 0, 12, 11, 5, 14, 9, 8, 3, 9, 7, 4, 8, 13, 6, 2, 1, 6, 11, 12, 2, 3, 0, 5,
14, 10, 13, 15, 4, 13, 3, 4, 9, 6, 10, 1, 12, 11, 0, 2, 5, 0, 13, 14, 2, 8, 15, 7, 4, 15,
1, 10, 7, 5, 6, 12, 11, 3, 8, 9, 14,
],
[
10, 13, 1, 11, 6, 8, 11, 5, 9, 4, 12, 2, 15, 3, 2, 14, 0, 6, 13, 1, 3, 15, 4, 10, 14, 9, 7,
12, 5, 0, 8, 7, 13, 1, 2, 4, 3, 6, 12, 11, 0, 13, 5, 14, 6, 8, 15, 2, 7, 10, 8, 15, 4, 9,
11, 5, 9, 0, 14, 3, 10, 7, 1, 12,
],
[
15, 0, 9, 5, 6, 10, 12, 9, 8, 7, 2, 12, 3, 13, 5, 2, 1, 14, 7, 8, 11, 4, 0, 3, 14, 11, 13,
6, 4, 1, 10, 15, 3, 13, 12, 11, 15, 3, 6, 0, 4, 10, 1, 7, 8, 4, 11, 14, 13, 8, 0, 6, 2, 15,
9, 5, 7, 1, 10, 12, 14, 2, 5, 9,
],
[
14, 4, 3, 15, 2, 13, 5, 3, 13, 14, 6, 9, 11, 2, 0, 5, 4, 1, 10, 12, 15, 6, 9, 10, 1, 8, 12,
7, 8, 11, 7, 0, 0, 15, 10, 5, 14, 4, 9, 10, 7, 8, 12, 3, 13, 1, 3, 6, 15, 12, 6, 11, 2, 9,
5, 0, 4, 2, 11, 14, 1, 7, 8, 13,
],
];
// custom
pub const PBOX: [u8; 32] = [
15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9, 1, 7, 23, 13, 31, 26, 2, 8, 18, 12,
29, 5, 21, 10, 3, 24,
];
// custom
pub const IP: [u8; 64] = [
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63,
55, 47, 39, 31, 23, 15, 7, 56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52,
44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6,
];
// custom
pub const IP_INV: [u8; 64] = [
39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24,
];
// custom
pub const KEY_PERMUTATION_TABLE: [u8; 56] = [
//key_param_c + key_param_d
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59,
51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3,
];
// custom
pub const KEY_COMPRESSION: [u8; 64] = [
13, 16, 10, 23, 0, 4, 255, 255, 2, 27, 14, 5, 20, 9, 255, 255, 22, 18, 11, 3, 25, 7, 255, 255,
15, 6, 26, 19, 12, 1, 255, 255, 40, 51, 30, 36, 46, 54, 255, 255, 29, 39, 50, 44, 32, 47, 255,
255, 43, 48, 38, 55, 33, 52, 255, 255, 45, 41, 49, 35, 28, 31, 255, 255,
];
// custom
pub const KEY_EXPANSION: [u8; 64] = [
31, 0, 1, 2, 3, 4, 255, 255, 3, 4, 5, 6, 7, 8, 255, 255, 7, 8, 9, 10, 11, 12, 255, 255, 11, 12,
13, 14, 15, 16, 255, 255, 15, 16, 17, 18, 19, 20, 255, 255, 19, 20, 21, 22, 23, 24, 255, 255,
23, 24, 25, 26, 27, 28, 255, 255, 27, 28, 29, 30, 31, 30, 255, 255,
];
pub const U64_SHIFT_TABLE_CACHE: [u64; 64] = {
let mut data = [0u64; 64];
let mut i = 0;
while i < 32 {
data[i] = 1u64 << i;
data[i + 32] = 1u64 << (i + 32);
i += 1;
}
data
};

View File

@ -1,49 +1,49 @@
pub const fn make_u64(hi32: u32, lo32: u32) -> u64 {
((hi32 as u64) << 32) | (lo32 as u64)
}
pub const fn swap_u64_side(value: u64) -> u64 {
(value.wrapping_shr(32)) | (value.wrapping_shl(32))
}
pub const fn u64_get_lo32(value: u64) -> u32 {
value as u32
}
pub const fn u64_get_hi32(value: u64) -> u32 {
value.wrapping_shr(32) as u32
}
pub fn get_u64_by_shift_idx(value: u8) -> u64 {
if value == 255 {
return 0;
}
if cfg!(target_pointer_width = "64") {
1u64.wrapping_shl(value as u32)
} else {
super::constants::U64_SHIFT_TABLE_CACHE
.get(value as usize)
.copied()
.unwrap_or_default()
}
}
#[test]
fn test_get_u64_by_shift_idx() {
assert_eq!(get_u64_by_shift_idx(0), 1);
assert_eq!(get_u64_by_shift_idx(63), 0x8000000000000000);
}
pub fn map_u64(src_value: u64, table: &[u8]) -> u64 {
table.iter().enumerate().fold(0u64, |acc, (i, &idx)| {
match get_u64_by_shift_idx(idx) & src_value {
0 => acc,
_ => acc | get_u64_by_shift_idx(i as u8),
}
})
}
pub fn map_u32(src_value: u32, table: &[u8]) -> u32 {
map_u64(src_value as u64, table) as u32
}
pub const fn make_u64(hi32: u32, lo32: u32) -> u64 {
((hi32 as u64) << 32) | (lo32 as u64)
}
pub const fn swap_u64_side(value: u64) -> u64 {
(value.wrapping_shr(32)) | (value.wrapping_shl(32))
}
pub const fn u64_get_lo32(value: u64) -> u32 {
value as u32
}
pub const fn u64_get_hi32(value: u64) -> u32 {
value.wrapping_shr(32) as u32
}
pub fn get_u64_by_shift_idx(value: u8) -> u64 {
if value == 255 {
return 0;
}
if cfg!(target_pointer_width = "64") {
1u64.wrapping_shl(value as u32)
} else {
super::constants::U64_SHIFT_TABLE_CACHE
.get(value as usize)
.copied()
.unwrap_or_default()
}
}
#[test]
fn test_get_u64_by_shift_idx() {
assert_eq!(get_u64_by_shift_idx(0), 1);
assert_eq!(get_u64_by_shift_idx(63), 0x8000000000000000);
}
pub fn map_u64(src_value: u64, table: &[u8]) -> u64 {
table.iter().enumerate().fold(0u64, |acc, (i, &idx)| {
match get_u64_by_shift_idx(idx) & src_value {
0 => acc,
_ => acc | get_u64_by_shift_idx(i as u8),
}
})
}
pub fn map_u32(src_value: u32, table: &[u8]) -> u32 {
map_u64(src_value as u64, table) as u32
}

View File

@ -1,57 +1,49 @@
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),
);
/// Decrypt string content
pub fn decrypt_ksing(data: &str, key: &[u8; 8]) -> Result<String> {
let mut decoded = B64.decode(data)?;
let des = KuwoDes::new(key, Mode::Decrypt);
des.transform(&mut decoded[..])?;
let result = String::from_utf8_lossy(&decoded[..])
.trim_end_matches('\x00')
.to_string();
Ok(result)
}
pub fn encrypt_ksing<T: AsRef<[u8]>>(data: T, key: &[u8; 8]) -> Result<String> {
let mut data = Vec::from(data.as_ref());
let padded_len = ((data.len() + 7) / 8) * 8;
data.resize(padded_len, 0u8);
let des = KuwoDes::new(key, Mode::Encrypt);
des.transform(&mut data[..])?;
Ok(B64.encode(data))
}
pub fn decode_ekey(data: &str, key: &[u8; 8]) -> Result<String> {
let decoded = decrypt_ksing(data, key)?;
Ok(decoded[16..].to_string())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ksing_decode() {
let expected = "hello world";
let decoded =
decrypt_ksing("tx5ct5ilzeLs7pN1C4RI6w==", b"12345678").expect("decrypt failed");
assert_eq!(decoded, expected);
}
}
use anyhow::Result;
mod constants;
mod core;
mod helper;
use core::{KuwoDes, Mode};
use umc_utils::base64;
/// Decrypt string content
pub fn decrypt_ksing(data: &str, key: &[u8; 8]) -> Result<String> {
let mut decoded = base64::decode(data)?;
let des = KuwoDes::new(key, Mode::Decrypt);
des.transform(&mut decoded[..])?;
let result = String::from_utf8_lossy(&decoded[..])
.trim_end_matches('\x00')
.to_string();
Ok(result)
}
pub fn encrypt_ksing<T: AsRef<[u8]>>(data: T, key: &[u8; 8]) -> Result<String> {
let mut data = Vec::from(data.as_ref());
let padded_len = ((data.len() + 7) / 8) * 8;
data.resize(padded_len, 0u8);
let des = KuwoDes::new(key, Mode::Encrypt);
des.transform(&mut data[..])?;
Ok(base64::encode(data))
}
pub fn decode_ekey(data: &str, key: &[u8; 8]) -> Result<String> {
let decoded = decrypt_ksing(data, key)?;
Ok(decoded[16..].to_string())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ksing_decode() {
let expected = "hello world";
let decoded =
decrypt_ksing("tx5ct5ilzeLs7pN1C4RI6w==", b"12345678").expect("decrypt failed");
assert_eq!(decoded, expected);
}
}

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;