diff options
| -rw-r--r-- | Cargo.lock | 41 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/Cargo.toml | 4 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/hash.rs | 27 |
4 files changed, 55 insertions, 21 deletions
@@ -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]] @@ -1575,17 +1575,6 @@ dependencies = [ [[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" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" @@ -1607,6 +1596,17 @@ dependencies = [ ] [[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" dependencies = [ @@ -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", ] @@ -2000,6 +2004,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2671,7 +2684,7 @@ dependencies = [ "memchr", "pbkdf2", "ppmd-rust", - "sha1 0.11.0", + "sha1", "time", "typed-path", "zeroize", @@ -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<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), + } } |
