diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-21 19:28:50 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-21 19:28:50 +0900 |
| commit | 1deb0b88edd455142160c0a03c094422907f2df6 (patch) | |
| tree | a04fe95f60c7289ab4d34ed04a540a1e029d9ad5 /crates | |
| parent | 9a60b4d6e2ca0c87bf767d31299caae0b91c2706 (diff) | |
| download | php-shirabe-1deb0b88edd455142160c0a03c094422907f2df6.tar.gz php-shirabe-1deb0b88edd455142160c0a03c094422907f2df6.tar.zst php-shirabe-1deb0b88edd455142160c0a03c094422907f2df6.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/Cargo.toml | 4 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/hash.rs | 27 |
2 files changed, 25 insertions, 6 deletions
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<u8> { + calculate_hash(algo, data.as_bytes()) } -pub fn hash_raw(_algo: &str, _data: &str) -> Vec<u8> { - todo!() +pub fn hash_file(algo: &str, filename: impl AsRef<std::path::Path>) -> Option<String> { + let data = std::fs::read(filename).ok()?; + Some(bin2hex(&calculate_hash(algo, &data))) } -pub fn hash_file(_algo: &str, _filename: &str) -> Option<String> { - todo!() +fn calculate_hash(algo: &str, data: &[u8]) -> Vec<u8> { + 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), + } } |
