diff rust/hg-core/src/revlog/revlog.rs @ 47380: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 e8ae91b1a63d
children 8c29af0f6d6e
line wrap: on
line diff
--- 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)]