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) --- crates/shirabe-php-shim/src/hash.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'crates/shirabe-php-shim/src') 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