feat(hash):完成基本散列算法。

This commit is contained in:
徐涛
2023-06-29 22:14:44 +08:00
parent 418fc4dccf
commit 260f17021b
4 changed files with 57 additions and 4 deletions

View File

@@ -0,0 +1,28 @@
pub mod md5 {
use md5::Digest;
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = md5::Md5::new();
hasher.update(input);
let result = hasher.finalize();
crate::serialize::to_hex(result.as_slice())
}
}
pub mod sha1 {
use sha1::Digest;
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = sha1::Sha1::new();
hasher.update(input);
let result = hasher.finalize();
crate::serialize::to_hex(result.as_slice())
}
}
pub mod sha512 {
pub fn hash<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = hmac_sha512::Hash::new();
hasher.update(input);
let result = hasher.finalize();
crate::serialize::to_hex(result.as_slice())
}
}

View File

@@ -0,0 +1,5 @@
pub mod uuid {
pub fn new() -> Box<String> {
Box::from(uuid::Uuid::new_v4().to_string())
}
}