fs_chunker/
lib.rs

1use std::{io::Read, path::Path};
2use sha256::digest;
3pub struct Chunk {
4    pub data: Vec<u8>,
5    pub hash: String,
6    pub idx: usize
7}
8
9pub fn chunk_file<P>(path: P, chunk_size: usize, use_blake: bool) -> Vec<Chunk> where P: AsRef<Path> {
10    let mut file = std::fs::File::open(path).unwrap();
11    let mut output: Vec<Chunk> = Vec::new();
12    let mut idx = 0;
13    loop {
14        let mut chunk: Vec<u8> = Vec::with_capacity(chunk_size);
15        let n = file.by_ref().take(chunk_size as u64).read_to_end(&mut chunk).unwrap();
16        if n == 0 { break; } // nothing read
17        let hash;
18        if use_blake {
19            hash = blake3::hash(&chunk).to_string();
20        }
21        else {
22            hash = digest(chunk.clone());
23        }
24        output.push(Chunk { data: chunk, hash, idx });
25        idx += 1;
26        if n < chunk_size { break; } // if we read less bytes than chunk size, exit loop too
27    }
28
29    output
30}
31
32pub fn hash_file<P>(path: P, chunk_size: usize, use_blake: bool) -> Vec<String> where P: AsRef<Path> {
33    let mut file = std::fs::File::open(path).unwrap();
34    let mut output: Vec<String> = Vec::new();
35
36    loop {
37        let mut chunk: Vec<u8> = Vec::with_capacity(chunk_size);
38        let n = file.by_ref().take(chunk_size as u64).read_to_end(&mut chunk).unwrap();
39        if n == 0 { break; } // nothing read
40        let hash: String;
41        if use_blake {
42            hash = blake3::hash(&chunk).to_string();
43        }
44        else {
45            hash = digest(chunk.clone());
46        }
47        output.push(hash);
48        if n < chunk_size { break; } // if we read less bytes than chunk size, exit loop too
49    }
50
51    output
52}