Compare commits
5 Commits
0.1.0
...
0697e61f35
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0697e61f35 | ||
|
|
a89f9aff15 | ||
|
|
51a7a48962 | ||
|
|
3f86e75518 | ||
|
|
dbcdfc426e |
@@ -11,6 +11,7 @@ crate-type = ["dylib", "rlib"]
|
||||
[dependencies]
|
||||
aes = "0.8.3"
|
||||
base64 = "0.21.2"
|
||||
blake2b_simd = "1.0.3"
|
||||
blockhash = "0.5.0"
|
||||
cbc = { version = "0.1.2", features = ["std"] }
|
||||
chrono = "0.4.26"
|
||||
|
||||
16
README.md
16
README.md
@@ -21,13 +21,18 @@ Rust 中可以使用的常用辅助功能工具箱。主要配备以下功能:
|
||||
- [x] 2048 位长
|
||||
- [x] KeyPair 生成器
|
||||
- 散列算法。
|
||||
- [x] Sha512 散列算法
|
||||
- [x] Sha1 散列算法
|
||||
- [x] MD5 散列算法
|
||||
- [x] 图像感知散列算法
|
||||
- [x] Sha512 散列算法(便捷封装)
|
||||
- [x] Sha1 散列算法(便捷封装)
|
||||
- [x] MD5 散列算法(便捷封装)
|
||||
- [x] 图像感知散列算法(便捷封装)
|
||||
- [ ] BLAKE2b 校验和算法(便捷封装)
|
||||
- [ ] BLAKE3 校验和算法(便捷封装)
|
||||
- 唯一序列号生成器
|
||||
- [x] 冰雹 ID 生成器(短主机精简日期版雪花 ID)
|
||||
- [x] UUID 生成器
|
||||
- [x] UUIDv4 生成器
|
||||
- [ ] UUIDv7 生成器(自定义时间戳分布式版本)
|
||||
- [ ] UUIDv7 比较及排序
|
||||
- [ ] 基于 Base36 的 short UUIDv7 转换器
|
||||
- [x] short UUID 生成器
|
||||
- 签名算法
|
||||
- [x] RSA 签名算法
|
||||
@@ -35,6 +40,7 @@ Rust 中可以使用的常用辅助功能工具箱。主要配备以下功能:
|
||||
- [x] 随机验证码生成算法
|
||||
- 序列化算法
|
||||
- [x] Base64 算法
|
||||
- [ ] Base36 算法
|
||||
- [x] Hex 直转
|
||||
- 常用工具函数
|
||||
- [x] 日期时间函数
|
||||
|
||||
@@ -40,7 +40,7 @@ pub fn encrypt(data: String) -> String {
|
||||
/// - `data` 待解密的内容
|
||||
pub fn decrypt(data: String) -> Result<String, SpiralCipherError> {
|
||||
if !data.starts_with("[") || data.len() <= 21 {
|
||||
return Err(SpiralCipherError::CorruptedCipherData);
|
||||
return Ok(data);
|
||||
}
|
||||
let data = data[1..].to_string();
|
||||
let key_seed = data[0..20].to_string();
|
||||
|
||||
97
src/hash/blake2b/mod.rs
Normal file
97
src/hash/blake2b/mod.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use blake2b_simd::{blake2b as blake2b_hasher, Params, State};
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
/// 根据给定的位数返回一个Blake2b散列实例。
|
||||
fn hasher_select(bit_size: usize) -> State {
|
||||
match bit_size {
|
||||
224 => Params::new().hash_length(28).to_state(), // 224 bits = 28 bytes
|
||||
384 => Params::new().hash_length(48).to_state(), // 384 bits = 48 bytes
|
||||
512 => Params::new().hash_length(64).to_state(), // 512 bits = 64 bytes
|
||||
256 | _ => Params::new().hash_length(32).to_state(), // 256 bits = 32 bytes
|
||||
}
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b校验和,返回字节数组。
|
||||
pub fn blake2b(data: &[u8]) -> Vec<u8> {
|
||||
blake2b_hasher(data).as_bytes().to_vec()
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/256校验和,返回字节数组。
|
||||
pub fn blake2b_256(data: &[u8]) -> Vec<u8> {
|
||||
let mut hasher = hasher_select(256);
|
||||
hasher.update(data);
|
||||
hasher.finalize().as_bytes().to_vec()
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/384校验和,返回字节数组。
|
||||
pub fn blake2b_384(data: &[u8]) -> Vec<u8> {
|
||||
let mut hasher = hasher_select(384);
|
||||
hasher.update(data);
|
||||
hasher.finalize().as_bytes().to_vec()
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/224校验和,返回字节数组。
|
||||
pub fn blake2b_224(data: &[u8]) -> Vec<u8> {
|
||||
let mut hasher = hasher_select(224);
|
||||
hasher.update(data);
|
||||
hasher.finalize().as_bytes().to_vec()
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b校验和,返回十六进制字符串。
|
||||
pub fn blake2b_hex(data: &[u8]) -> String {
|
||||
hex::encode(blake2b(data))
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/256校验和,返回十六进制字符串。
|
||||
pub fn blake2b_256_hex(data: &[u8]) -> String {
|
||||
hex::encode(blake2b_256(data))
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/384校验和,返回十六进制字符串。
|
||||
pub fn blake2b_384_hex(data: &[u8]) -> String {
|
||||
hex::encode(blake2b_384(data))
|
||||
}
|
||||
|
||||
/// 计算给定字节数组的Blake2b/224校验和,返回十六进制字符串。
|
||||
pub fn blake2b_224_hex(data: &[u8]) -> String {
|
||||
hex::encode(blake2b_224(data))
|
||||
}
|
||||
|
||||
/// 根据给定位数计算一个字节数组的Blake2b校验和,返回字节数组。
|
||||
pub fn sum(data: &[u8], bit_size: Option<usize>) -> Vec<u8> {
|
||||
let size = bit_size.unwrap_or(512);
|
||||
let mut hasher = hasher_select(size);
|
||||
hasher.update(data);
|
||||
hasher.finalize().as_bytes().to_vec()
|
||||
}
|
||||
|
||||
/// 根据给定位数计算一个字节数组的Blake2b校验和,返回十六进制字符串。
|
||||
pub fn sum_hex(data: &[u8], bit_size: Option<usize>) -> String {
|
||||
hex::encode(sum(data, bit_size))
|
||||
}
|
||||
|
||||
/// 根据给定位数计算一个文件的Blake2b校验和,返回字节数组。
|
||||
pub fn sum_file<P: AsRef<Path>>(file_path: P, bit_size: Option<usize>) -> io::Result<Vec<u8>> {
|
||||
let size = bit_size.unwrap_or(512);
|
||||
let mut file = File::open(file_path)?;
|
||||
let mut hasher = hasher_select(size);
|
||||
|
||||
let mut buffer = [0; 8192];
|
||||
loop {
|
||||
let bytes_read = file.read(&mut buffer)?;
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buffer[..bytes_read]);
|
||||
}
|
||||
|
||||
Ok(hasher.finalize().as_bytes().to_vec())
|
||||
}
|
||||
|
||||
/// 根据给定位数计算一个文件的Blake2b校验和,返回十六进制字符串。
|
||||
pub fn sum_file_hex<P: AsRef<Path>>(file_path: P, bit_size: Option<usize>) -> io::Result<String> {
|
||||
let hash = sum_file(file_path, bit_size)?;
|
||||
Ok(hex::encode(hash))
|
||||
}
|
||||
@@ -93,3 +93,5 @@ pub mod image_hash {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod blake2b;
|
||||
|
||||
@@ -69,13 +69,6 @@ impl HailSerialCodeAlgorithm {
|
||||
current_time - *HAIL_PERIOD_START
|
||||
}
|
||||
|
||||
/// 判断指定时间戳是否比已经存储的最后一次使用的时间戳要大。否则时间发生了回拨。
|
||||
fn validate_timestamp(&self, timestamp: i64) -> bool {
|
||||
let last_timestamp = self.last_timestamp.clone();
|
||||
let last_timestamp = last_timestamp.lock().unwrap();
|
||||
timestamp >= *last_timestamp
|
||||
}
|
||||
|
||||
/// 生成一个64位长整型序列ID。
|
||||
pub fn generate_serial(&self) -> i64 {
|
||||
let last_timestamp = self.last_timestamp.clone();
|
||||
@@ -93,7 +86,7 @@ impl HailSerialCodeAlgorithm {
|
||||
std::thread::sleep(time::Duration::from_secs(1));
|
||||
continue;
|
||||
}
|
||||
} else if !self.validate_timestamp(timestamp) {
|
||||
} else if timestamp < *last_timestamp {
|
||||
std::thread::sleep(time::Duration::from_secs(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, NaiveDateTime, Utc};
|
||||
use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
|
||||
|
||||
/// 获取一个类型为`chrono::DateTime<chrono::FixedOffset>`类型的当前日期时间的实例。时间时区将自动被设置为东八区。
|
||||
pub fn now_asia_shanghai() -> DateTime<FixedOffset> {
|
||||
@@ -7,7 +7,7 @@ pub fn now_asia_shanghai() -> DateTime<FixedOffset> {
|
||||
}
|
||||
|
||||
/// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到指定时区的时间实例。
|
||||
pub fn shift_tz(datetime: DateTime<Utc>, zone: i64) -> DateTime<FixedOffset> {
|
||||
pub fn shift_tz<T: TimeZone>(datetime: DateTime<T>, zone: i64) -> DateTime<FixedOffset> {
|
||||
if zone.is_positive() {
|
||||
datetime.with_timezone(
|
||||
&FixedOffset::east_opt(Duration::hours(zone.abs()).num_seconds() as i32).unwrap(),
|
||||
@@ -20,7 +20,7 @@ pub fn shift_tz(datetime: DateTime<Utc>, zone: i64) -> DateTime<FixedOffset> {
|
||||
}
|
||||
|
||||
/// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到东八区的时间实例。
|
||||
pub fn shift_to_asia_shanghai(datetime: DateTime<Utc>) -> DateTime<FixedOffset> {
|
||||
pub fn shift_to_asia_shanghai<T: TimeZone>(datetime: DateTime<T>) -> DateTime<FixedOffset> {
|
||||
shift_tz(datetime, 8)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user