Compare commits
2 Commits
e6447fdd43
...
0.1.1
Author | SHA1 | Date | |
---|---|---|---|
|
dbcdfc426e | ||
|
d1c19355a5 |
@@ -1,5 +1,9 @@
|
|||||||
pub mod md5 {
|
pub mod md5 {
|
||||||
use md5::Digest;
|
use md5::Digest;
|
||||||
|
|
||||||
|
/// 计算输入内容的MD5哈希值,返回十六进制字符串。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
||||||
let mut hasher = md5::Md5::new();
|
let mut hasher = md5::Md5::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -7,6 +11,9 @@ pub mod md5 {
|
|||||||
crate::serialize::to_hex(result.as_slice())
|
crate::serialize::to_hex(result.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 计算输入内容的MD5哈希值,返回字节向量。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
||||||
let mut hasher = md5::Md5::new();
|
let mut hasher = md5::Md5::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -17,6 +24,10 @@ pub mod md5 {
|
|||||||
|
|
||||||
pub mod sha1 {
|
pub mod sha1 {
|
||||||
use sha1::Digest;
|
use sha1::Digest;
|
||||||
|
|
||||||
|
/// 计算输入内容的SHA1哈希值,返回十六进制字符串。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
||||||
let mut hasher = sha1::Sha1::new();
|
let mut hasher = sha1::Sha1::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -24,6 +35,9 @@ pub mod sha1 {
|
|||||||
crate::serialize::to_hex(result.as_slice())
|
crate::serialize::to_hex(result.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 计算输入内容的SHA1哈希值,返回字节向量。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
||||||
let mut hasher = sha1::Sha1::new();
|
let mut hasher = sha1::Sha1::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -33,6 +47,10 @@ pub mod sha1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod sha512 {
|
pub mod sha512 {
|
||||||
|
|
||||||
|
/// 计算输入内容的SHA512哈希值,返回十六进制字符串。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
|
||||||
let mut hasher = hmac_sha512::Hash::new();
|
let mut hasher = hmac_sha512::Hash::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -40,6 +58,9 @@ pub mod sha512 {
|
|||||||
crate::serialize::to_hex(result.as_slice())
|
crate::serialize::to_hex(result.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 计算输入内容的SHA512哈希值,返回字节向量。
|
||||||
|
///
|
||||||
|
/// - `input` 输入内容。
|
||||||
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
pub fn hash_hex<T: AsRef<[u8]>>(input: T) -> Vec<u8> {
|
||||||
let mut hasher = hmac_sha512::Hash::new();
|
let mut hasher = hmac_sha512::Hash::new();
|
||||||
hasher.update(input);
|
hasher.update(input);
|
||||||
@@ -56,6 +77,10 @@ pub mod image_hash {
|
|||||||
Detailed = 32,
|
Detailed = 32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 计算输入图片的区块感知哈希值,返回十六进制字符串。
|
||||||
|
///
|
||||||
|
/// - `input` 输入图片。
|
||||||
|
/// - `precision` 感知精度,感知精度越高,越能区分图片的细节,但是计算时间也越长。
|
||||||
pub fn hash_image<T: image::GenericImage<Pixel = image::Rgb<u8>>>(
|
pub fn hash_image<T: image::GenericImage<Pixel = image::Rgb<u8>>>(
|
||||||
input: &T,
|
input: &T,
|
||||||
precision: Precision,
|
precision: Precision,
|
||||||
|
@@ -69,13 +69,6 @@ impl HailSerialCodeAlgorithm {
|
|||||||
current_time - *HAIL_PERIOD_START
|
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。
|
/// 生成一个64位长整型序列ID。
|
||||||
pub fn generate_serial(&self) -> i64 {
|
pub fn generate_serial(&self) -> i64 {
|
||||||
let last_timestamp = self.last_timestamp.clone();
|
let last_timestamp = self.last_timestamp.clone();
|
||||||
@@ -93,7 +86,7 @@ impl HailSerialCodeAlgorithm {
|
|||||||
std::thread::sleep(time::Duration::from_secs(1));
|
std::thread::sleep(time::Duration::from_secs(1));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if !self.validate_timestamp(timestamp) {
|
} else if timestamp < *last_timestamp {
|
||||||
std::thread::sleep(time::Duration::from_secs(1));
|
std::thread::sleep(time::Duration::from_secs(1));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
pub mod hail;
|
pub mod hail;
|
||||||
|
|
||||||
pub mod uuid {
|
pub mod uuid {
|
||||||
|
/// 生成一个UUID v4字符串。
|
||||||
pub fn new() -> Box<String> {
|
pub fn new() -> Box<String> {
|
||||||
Box::from(uuid::Uuid::new_v4().to_string())
|
Box::from(uuid::Uuid::new_v4().to_string())
|
||||||
}
|
}
|
||||||
@@ -8,6 +9,8 @@ pub mod uuid {
|
|||||||
|
|
||||||
pub mod short_uuid {
|
pub mod short_uuid {
|
||||||
const STR_SRC: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
const STR_SRC: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||||
|
|
||||||
|
/// 生成一个基于UUID v4的短UUID字符串。
|
||||||
pub fn new(length: i16) -> Box<String> {
|
pub fn new(length: i16) -> Box<String> {
|
||||||
let length = if length < 2 {
|
let length = if length < 2 {
|
||||||
2
|
2
|
||||||
|
Reference in New Issue
Block a user