fn file_digest digest_alg static digest Algorithm file_path std path P

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
fn file_digest(digest_alg: &'static digest::Algorithm, file_path: &std::path::Path) -> Result<Digest, std::string::String> {
std::fs::File::open(file_path).map_err(|why| {
format!("couldn't open {}: {}", file_path.display(), why.description())
}).and_then(|mut file| {
let mut ctx:Result<digest::Context, std::string::String> = Ok(digest::Context::new(digest_alg));
let mut chunk = vec![0u8; 128 * 1024];
loop {
match file.read(&mut chunk[..]) {
Ok(0) => break,
Ok(bytes_read) => ctx = ctx.map(|mut x| {x.update(&chunk[0..bytes_read]); x}),
Err(why) => {
ctx = Err(format!("couldn't read {}: {}", file_path.display(), why.description()));
break
},
}
}
ctx.map(|x| {x.finish()})
})
}