Is there a method to calculate the sha256 in the specified byte range of the file?

My code like:

use sha2::{Digest, Sha256};
use std::{fs::File, io};

pub fn sha256_by_file_path(file_path: &str) -> String {
  let file = File::open(file_path);
  let mut hasher = Sha256::new();
  io::copy(&mut file, &mut hasher).unwrap();
  let hash = hasher.finalize();
  let fh = format!("{:x}", hash);
  return fh;
}

I would like to get a sha256 of the part of the whole file.
For example, there is a file has 123456789 bytes, I want get the sha256 of the range 100 to 1234567800.

Thank you.

You can do this by seek()ing to byte position 100 in the file, then .take()ing the difference. Playground, with improved, more idiomatic code.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.