diff --git a/README.md b/README.md index 3cf97a7..f39e479 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ Rust 中可以使用的常用辅助功能工具箱。主要配备以下功能: - [ ] Pkcs7Padding - [ ] 3DES 便捷加解密算法 - 散列算法。 - - [ ] Sha512 散列算法 - - [ ] Sha1 散列算法 - - [ ] MD5 散列算法 + - [x] Sha512 散列算法 + - [x] Sha1 散列算法 + - [x] MD5 散列算法 - [ ] 图像感知散列算法 - 唯一序列号生成器 - [ ] 改进版雪花 ID 生成器(短主机精简日期版) - - [ ] UUID 生成器 + - [x] UUID 生成器 - [ ] short UUID 生成器 - 签名算法 - [ ] RSA 签名算法 diff --git a/src/hash/mod.rs b/src/hash/mod.rs index e69de29..69fb356 100644 --- a/src/hash/mod.rs +++ b/src/hash/mod.rs @@ -0,0 +1,28 @@ +pub mod md5 { + use md5::Digest; + pub fn hash>(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>(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>(input: T) -> String { + let mut hasher = hmac_sha512::Hash::new(); + hasher.update(input); + let result = hasher.finalize(); + crate::serialize::to_hex(result.as_slice()) + } +} diff --git a/src/serial_code/mod.rs b/src/serial_code/mod.rs index e69de29..ab9b9da 100644 --- a/src/serial_code/mod.rs +++ b/src/serial_code/mod.rs @@ -0,0 +1,5 @@ +pub mod uuid { + pub fn new() -> Box { + Box::from(uuid::Uuid::new_v4().to_string()) + } +} diff --git a/tests/hash.rs b/tests/hash.rs new file mode 100644 index 0000000..871afee --- /dev/null +++ b/tests/hash.rs @@ -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"); + } +}