Compare commits

3 Commits

4 changed files with 30 additions and 9 deletions

View File

@@ -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();

View File

@@ -1,5 +1,9 @@
pub mod md5 {
use md5::Digest;
/// 计算输入内容的MD5哈希值返回十六进制字符串。
///
/// - `input` 输入内容。
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = md5::Md5::new();
hasher.update(input);
@@ -7,6 +11,9 @@ pub mod md5 {
crate::serialize::to_hex(result.as_slice())
}
/// 计算输入内容的MD5哈希值返回字节向量。
///
/// - `input` 输入内容。
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
let mut hasher = md5::Md5::new();
hasher.update(input);
@@ -17,6 +24,10 @@ pub mod md5 {
pub mod sha1 {
use sha1::Digest;
/// 计算输入内容的SHA1哈希值返回十六进制字符串。
///
/// - `input` 输入内容。
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = sha1::Sha1::new();
hasher.update(input);
@@ -24,6 +35,9 @@ pub mod sha1 {
crate::serialize::to_hex(result.as_slice())
}
/// 计算输入内容的SHA1哈希值返回字节向量。
///
/// - `input` 输入内容。
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
let mut hasher = sha1::Sha1::new();
hasher.update(input);
@@ -33,6 +47,10 @@ pub mod sha1 {
}
pub mod sha512 {
/// 计算输入内容的SHA512哈希值返回十六进制字符串。
///
/// - `input` 输入内容。
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = hmac_sha512::Hash::new();
hasher.update(input);
@@ -40,6 +58,9 @@ pub mod sha512 {
crate::serialize::to_hex(result.as_slice())
}
/// 计算输入内容的SHA512哈希值返回字节向量。
///
/// - `input` 输入内容。
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
let mut hasher = hmac_sha512::Hash::new();
hasher.update(input);
@@ -56,6 +77,10 @@ pub mod image_hash {
Detailed = 32,
}
/// 计算输入图片的区块感知哈希值,返回十六进制字符串。
///
/// - `input` 输入图片。
/// - `precision` 感知精度,感知精度越高,越能区分图片的细节,但是计算时间也越长。
pub fn hash_image<T: image::GenericImage<Pixel = image::Rgb<u8>>>(
input: &T,
precision: Precision,

View File

@@ -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;
}

View File

@@ -1,6 +1,7 @@
pub mod hail;
pub mod uuid {
/// 生成一个UUID v4字符串。
pub fn new() -> Box<String> {
Box::from(uuid::Uuid::new_v4().to_string())
}
@@ -8,6 +9,8 @@ pub mod uuid {
pub mod short_uuid {
const STR_SRC: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
/// 生成一个基于UUID v4的短UUID字符串。
pub fn new(length: i16) -> Box<String> {
let length = if length < 2 {
2