Untitled
unknown
rust
a year ago
1.2 kB
10
Indexable
use core::hash;
use std::fs::File;
use std::io::{BufReader, Read};
use sha3::{Digest, Sha3_256};
#[derive(Default)]
pub struct SpecialFile {
pub path: String,
pub blocks: Vec<FileBlock>
}
pub struct FileBlock {
pub start_byte : usize,
pub end_byte: usize,
pub hash_value: String
}
pub fn generate_blocks(file_path: &str) -> SpecialFile {
let file = File::open(file_path).unwrap();
let mut reader = BufReader::new(file);
const BLOCK_SIZE: usize = 4096;
let mut buffer = [0u8; BLOCK_SIZE];
let mut special_file = SpecialFile {
path: file_path.to_string(),
blocks: Vec::new()
};
let mut start_byte = 0usize;
let mut end_byte;
loop {
let bytes_read = reader.read(&mut buffer).unwrap_or_default();
if bytes_read == 0 {
break;
}
let mut hasher = Sha3_256::new();
hasher.update(&buffer[..bytes_read]);
let hash_result = hasher.finalize();
end_byte = start_byte + bytes_read;
special_file.blocks.push(FileBlock { start_byte: start_byte, end_byte: end_byte, hash_value: hex::encode(hash_result) });
start_byte += bytes_read
}
special_file
}Editor is loading...
Leave a Comment