xor_str/lib.rs
1
2//! # XOR 编译期加密字符串并且运行时自动解密
3//!
4//! # XOR encrypts strings at compile time and decrypts them automatically at run time
5//!
6//! 为什么要用这个?
7//!
8//! 原因:项目编译成机器码后,数据库密码或者sql语句等敏感信息,是暴露在机器码中的,
9//!
10//! 如果通过gbk编码强行打开该exe文件,通过搜索"mysql"关键字,即可看到数据库链接信息,包括您的密码
11//!
12//! 通过使用该依赖,就可以隐藏重要文本数据
13//!
14//! Why use this?
15//!
16//! Reason: After the project is compiled into machine code, sensitive information such as database passwords or sql statements are exposed to the machine code
17//!
18//! If you force open the exe file with gbk encoding, you can see the database link information, including your password, by searching for the keyword "mysql"
19//!
20//! By using this dependency, you can hide important text data
21//! # 使用方式 how to use
22//! ```
23//! [dependencies]
24//! xor-str = "*"
25//!
26//! use xor_str::xor;
27//! use xor_str::encode;
28//! use xor_str::decode;
29//! fn main() {
30//! println!("{}",xor!("Hello, world!"));
31//! }
32//! ```
33pub use xor_str_encode::encode;
34
35///xor宏
36/// 在这里加密并解密,加密是过程宏,编译期间执行
37
38#[macro_export]
39macro_rules! xor {
40 ($str:literal) => {
41 decode(encode!($str))
42 };
43}
44
45///具体的xor解密逻辑
46pub fn decode(str: &[u8]) -> String {
47 let len = str.len();
48 let mut decrypted = Vec::with_capacity(len);
49 for (i, v) in str.iter().enumerate() {
50 decrypted.push(v ^ (len + i ) as u8);
51 }
52 String::from_utf8(decrypted).unwrap()
53}
54
55///创建并通过xor方式加密字符串
56#[cfg(test)]
57mod tests {
58 use crate::decode;
59 use crate::encode;
60 #[test]
61 fn it_works() {
62 println!("{}", xor!(br#"hello word!"#));
63 println!("{}", xor!("你好 世界!"));
64 }
65}