From 1deb0b88edd455142160c0a03c094422907f2df6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 21 Jun 2026 19:28:50 +0900 Subject: feat(php-shim): implement hash, hash_raw, hash_file Support md5, sha1, sha256, and xxh3 algorithms. Co-Authored-By: Claude Opus 4.8 (1M context) --- Cargo.lock | 41 ++++++++++++++++++++++++------------- Cargo.toml | 4 +++- crates/shirabe-php-shim/Cargo.toml | 4 ++++ crates/shirabe-php-shim/src/hash.rs | 27 ++++++++++++++++++------ 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0702269..1f75c36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -984,7 +984,7 @@ version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47bb1e988e6fb779cf720ad431242d3f03167c1b3f2b1aae7f1a94b2495b36ae" dependencies = [ - "sha2", + "sha2 0.10.9", ] [[package]] @@ -1573,17 +1573,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha1" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" -dependencies = [ - "cfg-if", - "cpufeatures 0.2.17", - "digest 0.10.7", -] - [[package]] name = "sha1" version = "0.11.0" @@ -1606,6 +1595,17 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "sha2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "digest 0.11.3", +] + [[package]] name = "shirabe" version = "0.0.1" @@ -1619,7 +1619,7 @@ dependencies = [ "regex", "serde", "serde_json", - "sha1 0.10.6", + "sha1", "shirabe-class-map-generator", "shirabe-external-packages", "shirabe-php-shim", @@ -1659,11 +1659,15 @@ dependencies = [ "chrono", "fastrand", "indexmap", + "md5", "regex", "reqwest", "serde", "serde_json", "serde_urlencoded", + "sha1", + "sha2 0.11.0", + "twox-hash", "zip", ] @@ -1999,6 +2003,15 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "twox-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" +dependencies = [ + "rand", +] + [[package]] name = "typed-path" version = "0.12.3" @@ -2671,7 +2684,7 @@ dependencies = [ "memchr", "pbkdf2", "ppmd-rust", - "sha1 0.11.0", + "sha1", "time", "typed-path", "zeroize", diff --git a/Cargo.toml b/Cargo.toml index 1898718..544bf3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,9 +24,11 @@ reqwest = "0.13.4" serde = { version = "1.0.228", features = ["derive"] } serde_json = { version = "1.0.149", features = ["preserve_order"] } serde_urlencoded = "0.7.1" -sha1 = "0.10.6" +sha1 = "0.11.0" +sha2 = "0.11.0" tempfile = "3.27.0" tokio = { version = "1.52.3", features = ["full"] } +twox-hash = "2.1.2" url = "2.5.8" zip = "8.6.0" diff --git a/crates/shirabe-php-shim/Cargo.toml b/crates/shirabe-php-shim/Cargo.toml index bd285f5..4226234 100644 --- a/crates/shirabe-php-shim/Cargo.toml +++ b/crates/shirabe-php-shim/Cargo.toml @@ -8,11 +8,15 @@ anyhow.workspace = true chrono.workspace = true fastrand.workspace = true indexmap.workspace = true +md5.workspace = true regex.workspace = true reqwest.workspace = true serde.workspace = true serde_json.workspace = true serde_urlencoded.workspace = true +sha1.workspace = true +sha2.workspace = true +twox-hash.workspace = true zip.workspace = true [lints] diff --git a/crates/shirabe-php-shim/src/hash.rs b/crates/shirabe-php-shim/src/hash.rs index 45d53c0..6e12a81 100644 --- a/crates/shirabe-php-shim/src/hash.rs +++ b/crates/shirabe-php-shim/src/hash.rs @@ -1,11 +1,26 @@ -pub fn hash(_algo: &str, _data: &str) -> String { - todo!() +use crate::bin2hex; +use sha1::Digest as _; +use sha2::Digest as _; + +pub fn hash(algo: &str, data: &str) -> String { + crate::bin2hex(&calculate_hash(algo, data.as_bytes())) +} + +pub fn hash_raw(algo: &str, data: &str) -> Vec { + calculate_hash(algo, data.as_bytes()) } -pub fn hash_raw(_algo: &str, _data: &str) -> Vec { - todo!() +pub fn hash_file(algo: &str, filename: impl AsRef) -> Option { + let data = std::fs::read(filename).ok()?; + Some(bin2hex(&calculate_hash(algo, &data))) } -pub fn hash_file(_algo: &str, _filename: &str) -> Option { - todo!() +fn calculate_hash(algo: &str, data: &[u8]) -> Vec { + match algo { + "md5" => md5::compute(data).0.to_vec(), + "sha1" => sha1::Sha1::digest(data).to_vec(), + "sha256" => sha2::Sha256::digest(data).to_vec(), + "xxh3" => twox_hash::XxHash3_64::oneshot(data).to_be_bytes().to_vec(), + _ => panic!("unsupported hash algorithm: {}", algo), + } } -- cgit v1.3.1