doc:补充一些函数的文档。

This commit is contained in:
徐涛 2023-07-03 22:39:32 +08:00
parent e6447fdd43
commit d1c19355a5
2 changed files with 28 additions and 0 deletions

View File

@ -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,

View File

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