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

@ -12,13 +12,13 @@ Rust 中可以使用的常用辅助功能工具箱。主要配备以下功能:
- [ ] Pkcs7Padding
- [ ] 3DES 便捷加解密算法
- 散列算法。
- [ ] Sha512 散列算法
- [ ] Sha1 散列算法
- [ ] MD5 散列算法
- [x] Sha512 散列算法
- [x] Sha1 散列算法
- [x] MD5 散列算法
- [ ] 图像感知散列算法
- 唯一序列号生成器
- [ ] 改进版雪花 ID 生成器(短主机精简日期版)
- [ ] UUID 生成器
- [x] UUID 生成器
- [ ] short UUID 生成器
- 签名算法
- [ ] RSA 签名算法

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())
}
}

20
tests/hash.rs Normal file
View File

@ -0,0 +1,20 @@
#[cfg(test)]
mod hash {
#[test]
fn test_md5() {
let hashed_value = rs_toolbox::hash::md5::hash("hello");
assert_eq!(hashed_value, "5d41402abc4b2a76b9719d911017c592");
}
#[test]
fn test_sha1() {
let hashed_value = rs_toolbox::hash::sha1::hash("hello");
assert_eq!(hashed_value, "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d");
}
#[test]
fn test_sha512() {
let hashed_value = rs_toolbox::hash::sha512::hash("hello");
assert_eq!(hashed_value, "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043");
}
}