From 5090ee143298b67dcfc8294794887d2807ef1520 Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Thu, 12 Dec 2024 19:48:49 +0800 Subject: [PATCH] refactor: remove `sha1_smol` dependency --- mercury/Cargo.toml | 1 - mercury/src/hash.rs | 46 ++++++++++++++------------------------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/mercury/Cargo.toml b/mercury/Cargo.toml index 94c9d4de8..dca858a30 100644 --- a/mercury/Cargo.toml +++ b/mercury/Cargo.toml @@ -23,7 +23,6 @@ colored = { workspace = true } chrono = { workspace = true } tracing-subscriber = { workspace = true } uuid = { workspace = true, features = ["v4"] } -sha1_smol = "1.0.0" threadpool = "1.8.1" num_cpus.workspace = true dashmap = "6.0.1" diff --git a/mercury/src/hash.rs b/mercury/src/hash.rs index 5f346c227..a5d947010 100644 --- a/mercury/src/hash.rs +++ b/mercury/src/hash.rs @@ -7,7 +7,7 @@ use std::fmt::Display; use colored::Colorize; use serde::{Deserialize, Serialize}; -use sha1_smol::Digest; +use sha1::Digest; use crate::internal::object::types::ObjectType; @@ -44,7 +44,7 @@ impl Display for SHA1 { write!(f, "{}", hex::encode(self.0)) } } -impl AsRef<[u8]> for SHA1{ +impl AsRef<[u8]> for SHA1 { fn as_ref(&self) -> &[u8] { &self.0 } @@ -62,14 +62,11 @@ impl std::str::FromStr for SHA1 { fn from_str(s: &str) -> Result { let mut h = SHA1::default(); - - let d = Digest::from_str(s); - - match d { - Ok(d) => h.0.copy_from_slice(d.bytes().as_slice()), - Err(e) => return Err(e.to_string()), + if s.len() != 40 { + return Err("The length of the string is not 40".to_string()); } - + let bytes = hex::decode(s).map_err(|e| e.to_string())?; + h.0.copy_from_slice(bytes.as_slice()); Ok(h) } } @@ -98,14 +95,8 @@ impl std::str::FromStr for SHA1 { impl SHA1 { /// Calculate the SHA-1 hash of `Vec` data, then create a Hash value pub fn new(data: &Vec) -> SHA1 { - // Create a Sha1 object for calculating the SHA-1 hash - let s = sha1_smol::Sha1::from(data); - // Get the result of the hash - let sha1 = s.digest(); - // Convert the result to a 20-byte array - let result = sha1.bytes(); - - SHA1(result) + let h = sha1::Sha1::digest(data); + SHA1::from_bytes(h.as_slice()) } pub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> SHA1 { @@ -126,7 +117,7 @@ impl SHA1 { h } - /// Export sha1 value to String with the color + /// Export sha1 value to String with the color pub fn to_color_str(self) -> String { self.to_string().red().bold().to_string() } @@ -139,7 +130,7 @@ impl SHA1 { #[cfg(test)] mod tests { - + use std::io::BufReader; use std::io::Read; use std::io::Seek; @@ -188,10 +179,7 @@ mod tests { 0xf2, 0x56, 0x7c, 0x36, 0xda, 0x6d, ]); - assert_eq!( - sha1.to_string(), - "8ab686eafeb1f44702738c8b0f24f2567c36da6d" - ); + assert_eq!(sha1.to_string(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d"); } #[test] @@ -200,10 +188,7 @@ mod tests { match SHA1::from_str(hash_str) { Ok(hash) => { - assert_eq!( - hash.to_string(), - "8ab686eafeb1f44702738c8b0f24f2567c36da6d" - ); + assert_eq!(hash.to_string(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d"); } Err(e) => println!("Error: {}", e), } @@ -215,10 +200,7 @@ mod tests { match SHA1::from_str(hash_str) { Ok(hash) => { - assert_eq!( - hash.to_string(), - "8ab686eafeb1f44702738c8b0f24f2567c36da6d" - ); + assert_eq!(hash.to_string(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d"); } Err(e) => println!("Error: {}", e), } @@ -227,7 +209,7 @@ mod tests { #[test] fn test_sha1_to_data() { let hash_str = "8ab686eafeb1f44702738c8b0f24f2567c36da6d"; - + match SHA1::from_str(hash_str) { Ok(hash) => { assert_eq!(