random wallpaper rotator with tags and favorites
at main 884 B view raw
1use anyhow::{Context, Result}; 2use hex; 3use sha2::{Digest, Sha256}; 4use std::fs::{File}; 5use std::io::{BufReader, Read}; 6use std::path::Path; 7 8const HASH_BUFFER_SIZE: usize = 8192; 9 10pub fn calculate_image_hash(path: &Path) -> Result<String> { 11 let file = File::open(path) 12 .with_context(|| format!("Failed to open image file for hashing: {}", path.display()))?; 13 let mut reader = BufReader::with_capacity(HASH_BUFFER_SIZE, file); 14 let mut hasher = Sha256::new(); 15 let mut buffer = [0u8; HASH_BUFFER_SIZE]; 16 17 loop { 18 let bytes_read = reader.read(&mut buffer).with_context(|| { 19 format!("Failed to read image file for hashing: {}", path.display()) 20 })?; 21 if bytes_read == 0 { 22 break; 23 } 24 hasher.update(&buffer[..bytes_read]); 25 } 26 27 let hash_bytes = hasher.finalize(); 28 Ok(hex::encode(hash_bytes)) 29}