changeset 47386:fad504cfc94b

rust: Use a maintained crate for SHA-1 hashing https://crates.io/crates/rust-crypto hasn’t been updated in 5 years. This doesn’t neccesarily mean there’s anything wrong with it, but if something comes up it’s preferable to rely on libraries that have active maintainers. Use https://crates.io/crates/sha-1 from https://github.com/RustCrypto instead Differential Revision: https://phab.mercurial-scm.org/D10835
author Simon Sapin <simon.sapin@octobus.net>
date Wed, 02 Jun 2021 10:00:50 +0200
parents f6bb181c75f8
children 8aa3968c6d44
files rust/Cargo.lock rust/hg-core/Cargo.toml rust/hg-core/src/revlog/path_encode.rs rust/hg-core/src/revlog/revlog.rs
diffstat 4 files changed, 80 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Wed Jun 02 18:03:43 2021 +0200
+++ b/rust/Cargo.lock	Wed Jun 02 10:00:50 2021 +0200
@@ -57,6 +57,15 @@
 ]
 
 [[package]]
+name = "block-buffer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
 name = "byteorder"
 version = "1.3.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -138,6 +147,15 @@
 checksum = "cd51eab21ab4fd6a3bf889e2d0958c0a6e3a61ad04260325e919e652a2a62826"
 
 [[package]]
+name = "cpufeatures"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8"
+dependencies = [
+ "libc",
+]
+
+[[package]]
 name = "cpython"
 version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -254,6 +272,15 @@
 checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
 
 [[package]]
+name = "digest"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
 name = "either"
 version = "1.6.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -308,16 +335,14 @@
 ]
 
 [[package]]
-name = "fuchsia-cprng"
-version = "0.1.1"
+name = "generic-array"
+version = "0.14.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
-
-[[package]]
-name = "gcc"
-version = "0.3.55"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
+checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
+dependencies = [
+ "typenum",
+ "version_check",
+]
 
 [[package]]
 name = "getrandom"
@@ -364,13 +389,13 @@
  "memmap",
  "micro-timer",
  "pretty_assertions",
- "rand 0.7.3",
+ "rand",
  "rand_distr",
  "rand_pcg",
  "rayon",
  "regex",
- "rust-crypto",
  "same-file",
+ "sha-1",
  "tempfile",
  "twox-hash",
  "zstd",
@@ -413,7 +438,7 @@
 checksum = "3ca8957e71f04a205cb162508f9326aea04676c8dfd0711220190d6b83664f3f"
 dependencies = [
  "bitmaps",
- "rand_core 0.5.1",
+ "rand_core",
  "rand_xoshiro",
  "sized-chunks",
  "typenum",
@@ -563,6 +588,12 @@
 ]
 
 [[package]]
+name = "opaque-debug"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
+
+[[package]]
 name = "output_vt100"
 version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -666,29 +697,6 @@
 
 [[package]]
 name = "rand"
-version = "0.3.23"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
-dependencies = [
- "libc",
- "rand 0.4.6",
-]
-
-[[package]]
-name = "rand"
-version = "0.4.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
-dependencies = [
- "fuchsia-cprng",
- "libc",
- "rand_core 0.3.1",
- "rdrand",
- "winapi",
-]
-
-[[package]]
-name = "rand"
 version = "0.7.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
@@ -696,7 +704,7 @@
  "getrandom",
  "libc",
  "rand_chacha",
- "rand_core 0.5.1",
+ "rand_core",
  "rand_hc",
 ]
 
@@ -707,26 +715,11 @@
 checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
 dependencies = [
  "ppv-lite86",
- "rand_core 0.5.1",
+ "rand_core",
 ]
 
 [[package]]
 name = "rand_core"
-version = "0.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
-dependencies = [
- "rand_core 0.4.2",
-]
-
-[[package]]
-name = "rand_core"
-version = "0.4.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
-
-[[package]]
-name = "rand_core"
 version = "0.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
@@ -740,7 +733,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "96977acbdd3a6576fb1d27391900035bf3863d4a16422973a409b488cf29ffb2"
 dependencies = [
- "rand 0.7.3",
+ "rand",
 ]
 
 [[package]]
@@ -749,7 +742,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
 dependencies = [
- "rand_core 0.5.1",
+ "rand_core",
 ]
 
 [[package]]
@@ -758,7 +751,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
 dependencies = [
- "rand_core 0.5.1",
+ "rand_core",
 ]
 
 [[package]]
@@ -767,7 +760,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a9fcdd2e881d02f1d9390ae47ad8e5696a9e4be7b547a1da2afbc61973217004"
 dependencies = [
- "rand_core 0.5.1",
+ "rand_core",
 ]
 
 [[package]]
@@ -796,15 +789,6 @@
 ]
 
 [[package]]
-name = "rdrand"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
-dependencies = [
- "rand_core 0.3.1",
-]
-
-[[package]]
 name = "redox_syscall"
 version = "0.1.57"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -855,25 +839,6 @@
 ]
 
 [[package]]
-name = "rust-crypto"
-version = "0.2.36"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
-dependencies = [
- "gcc",
- "libc",
- "rand 0.3.23",
- "rustc-serialize",
- "time",
-]
-
-[[package]]
-name = "rustc-serialize"
-version = "0.3.24"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
-
-[[package]]
 name = "same-file"
 version = "1.0.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -889,6 +854,19 @@
 checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
 
 [[package]]
+name = "sha-1"
+version = "0.9.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c4cfa741c5832d0ef7fab46cabed29c2aae926db0b11bb2069edd8db5e64e16"
+dependencies = [
+ "block-buffer",
+ "cfg-if 1.0.0",
+ "cpufeatures",
+ "digest",
+ "opaque-debug",
+]
+
+[[package]]
 name = "sized-chunks"
 version = "0.6.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -929,7 +907,7 @@
 dependencies = [
  "cfg-if 0.1.10",
  "libc",
- "rand 0.7.3",
+ "rand",
  "redox_syscall",
  "remove_dir_all",
  "winapi",
@@ -980,7 +958,7 @@
 checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59"
 dependencies = [
  "cfg-if 0.1.10",
- "rand 0.7.3",
+ "rand",
  "static_assertions",
 ]
 
--- a/rust/hg-core/Cargo.toml	Wed Jun 02 18:03:43 2021 +0200
+++ b/rust/hg-core/Cargo.toml	Wed Jun 02 10:00:50 2021 +0200
@@ -21,6 +21,7 @@
 rand_distr = "0.2.2"
 rayon = "1.3.0"
 regex = "1.3.9"
+sha-1 = "0.9.6"
 twox-hash = "1.5.0"
 same-file = "1.0.6"
 tempfile = "3.1.0"
@@ -29,7 +30,6 @@
 log = "0.4.8"
 memmap = "0.7.0"
 zstd = "0.5.3"
-rust-crypto = "0.2.36"
 format-bytes = "0.2.2"
 
 # We don't use the `miniz-oxide` backend to not change rhg benchmarks and until
--- a/rust/hg-core/src/revlog/path_encode.rs	Wed Jun 02 18:03:43 2021 +0200
+++ b/rust/hg-core/src/revlog/path_encode.rs	Wed Jun 02 10:00:50 2021 +0200
@@ -1,5 +1,4 @@
-use crypto::digest::Digest;
-use crypto::sha1::Sha1;
+use sha1::{Digest, Sha1};
 
 #[derive(PartialEq, Debug)]
 #[allow(non_camel_case_types)]
@@ -621,13 +620,7 @@
         panic!("path_encode::hash_encore: string too long: {}", baselen)
     };
     let dirlen = encode_dir(Some(&mut dired[..]), src);
-    let sha = {
-        let mut hasher = Sha1::new();
-        hasher.input(&dired[..dirlen]);
-        let mut hash = vec![0; 20];
-        hasher.result(&mut hash);
-        hash
-    };
+    let sha = Sha1::digest(&dired[..dirlen]);
     let lowerlen = lower_encode(Some(&mut lowered[..]), &dired[..dirlen][5..]);
     let auxlen = aux_encode(Some(&mut auxed[..]), &lowered[..lowerlen]);
     hash_mangle(&auxed[..auxlen], &sha)
--- a/rust/hg-core/src/revlog/revlog.rs	Wed Jun 02 18:03:43 2021 +0200
+++ b/rust/hg-core/src/revlog/revlog.rs	Wed Jun 02 10:00:50 2021 +0200
@@ -4,10 +4,9 @@
 use std::path::Path;
 
 use byteorder::{BigEndian, ByteOrder};
-use crypto::digest::Digest;
-use crypto::sha1::Sha1;
 use flate2::read::ZlibDecoder;
 use micro_timer::timed;
+use sha1::{Digest, Sha1};
 use zstd;
 
 use super::index::Index;
@@ -221,7 +220,7 @@
             None => &NULL_NODE,
         };
 
-        hash(data, h1.as_bytes(), h2.as_bytes()).as_slice() == expected
+        &hash(data, h1.as_bytes(), h2.as_bytes()) == expected
     }
 
     /// Build the full data of a revision out its snapshot
@@ -361,20 +360,22 @@
 }
 
 /// Calculate the hash of a revision given its data and its parents.
-fn hash(data: &[u8], p1_hash: &[u8], p2_hash: &[u8]) -> Vec<u8> {
+fn hash(
+    data: &[u8],
+    p1_hash: &[u8],
+    p2_hash: &[u8],
+) -> [u8; NODE_BYTES_LENGTH] {
     let mut hasher = Sha1::new();
     let (a, b) = (p1_hash, p2_hash);
     if a > b {
-        hasher.input(b);
-        hasher.input(a);
+        hasher.update(b);
+        hasher.update(a);
     } else {
-        hasher.input(a);
-        hasher.input(b);
+        hasher.update(a);
+        hasher.update(b);
     }
-    hasher.input(data);
-    let mut hash = vec![0; NODE_BYTES_LENGTH];
-    hasher.result(&mut hash);
-    hash
+    hasher.update(data);
+    *hasher.finalize().as_ref()
 }
 
 #[cfg(test)]