Skip to content

Commit

Permalink
Switch to xxHash algorithm (#20)
Browse files Browse the repository at this point in the history
* Switch to `xxHash` algorithm

* Remove redundant comment
  • Loading branch information
yury-fedotov authored Aug 3, 2024
1 parent 063b984 commit 7be0d3b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 66 deletions.
151 changes: 90 additions & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ ignore = "0.4"
colored = "2.0"
tempfile = "3.3"
num-format = "0.4"
sha2 = "0.10"
twox-hash = "1.6.3"
9 changes: 5 additions & 4 deletions src/file_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs;
use std::hash::Hasher;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use twox_hash::XxHash64;

#[derive(Clone)]
pub struct FileInfo {
Expand Down Expand Up @@ -32,16 +33,16 @@ pub fn get_file_size(path: &PathBuf) -> u64 {

pub fn calculate_hash(path: &PathBuf) -> io::Result<String> {
let mut file = fs::File::open(path)?;
let mut hasher = Sha256::new();
let mut hasher = XxHash64::with_seed(0);
let mut buffer = [0; 1024];
loop {
let n = file.read(&mut buffer)?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
hasher.write(&buffer[..n]);
}
let hash = hasher.finalize();
let hash = hasher.finish();
Ok(format!("{:x}", hash))
}

Expand Down

0 comments on commit 7be0d3b

Please sign in to comment.