Compare commits
4 Commits
e6447fdd43
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
51a7a48962 | ||
|
3f86e75518 | ||
|
dbcdfc426e | ||
|
d1c19355a5 |
@@ -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();
|
||||
|
@@ -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,
|
||||
|
@@ -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,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
|
||||
|
@@ -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