From 2d4c1ccc042fff22313799e6b68908c0eda1f73d Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 22 Nov 2025 19:38:55 +0800 Subject: Add SHA1 string hashing function --- crates/utils/sha1_hash/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/utils/sha1_hash/src/lib.rs b/crates/utils/sha1_hash/src/lib.rs index 4373d50..9963cbe 100644 --- a/crates/utils/sha1_hash/src/lib.rs +++ b/crates/utils/sha1_hash/src/lib.rs @@ -11,6 +11,18 @@ pub struct Sha1Result { pub hash: String, } +/// Calc SHA1 hash of a string +pub fn calc_sha1_string>(input: S) -> String { + let mut hasher = Sha1::new(); + hasher.update(input.as_ref().as_bytes()); + let hash_result = hasher.finalize(); + + hash_result + .iter() + .map(|b| format!("{:02x}", b)) + .collect::() +} + /// Calc SHA1 hash of a single file pub async fn calc_sha1>( path: P, @@ -103,6 +115,31 @@ mod tests { use super::*; use std::fs; + #[test] + fn test_sha1_string() { + let test_string = "Hello, SHA1!"; + let hash = calc_sha1_string(test_string); + + let expected_hash = "de1c3daadc6f0f1626f4cf56c03e05a1e5d7b187"; + + assert_eq!( + hash, expected_hash, + "SHA1 hash should be consistent for same input" + ); + } + + #[test] + fn test_sha1_string_empty() { + let hash = calc_sha1_string(""); + + // SHA1 of empty string is "da39a3ee5e6b4b0d3255bfef95601890afd80709" + let expected_empty_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; + assert_eq!( + hash, expected_empty_hash, + "SHA1 hash mismatch for empty string" + ); + } + #[tokio::test] async fn test_sha1_accuracy() { // Test file path relative to the crate root -- cgit