-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ebfull/hash-checks-of-params
Hash checks of parameter files during initialization
- Loading branch information
Showing
3 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use blake2_rfc::blake2b::Blake2b; | ||
use std::io::{self, Read}; | ||
|
||
/// Abstraction over a reader which hashes the data being read. | ||
pub struct HashReader<R: Read> { | ||
reader: R, | ||
hasher: Blake2b, | ||
} | ||
|
||
impl<R: Read> HashReader<R> { | ||
/// Construct a new `HashReader` given an existing `reader` by value. | ||
pub fn new(reader: R) -> Self { | ||
HashReader { | ||
reader: reader, | ||
hasher: Blake2b::new(64), | ||
} | ||
} | ||
|
||
/// Destroy this reader and return the hash of what was read. | ||
pub fn into_hash(self) -> String { | ||
let hash = self.hasher.finalize(); | ||
|
||
let mut s = String::new(); | ||
for c in hash.as_bytes().iter() { | ||
s += &format!("{:02x}", c); | ||
} | ||
|
||
s | ||
} | ||
} | ||
|
||
impl<R: Read> Read for HashReader<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
let bytes = self.reader.read(buf)?; | ||
|
||
if bytes > 0 { | ||
self.hasher.update(&buf[0..bytes]); | ||
} | ||
|
||
Ok(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters