dapps-fetcher: calculate keccak in-flight while reading the response (#8294)

* dapps-fetcher: calculate keccak in-flight while reading the response

* Rename keccak_buffer_and_write -> keccak_pipe

* Fix file read bug by creating another file handle as the return value
This commit is contained in:
Wei Tang
2018-04-03 20:58:55 +08:00
committed by Rando
parent 99a13c4e66
commit 5e7d42e4a4
2 changed files with 18 additions and 7 deletions

View File

@@ -52,7 +52,7 @@ pub fn write_keccak<T: AsRef<[u8]>>(s: T, dest: &mut [u8]) {
}
}
pub fn keccak_buffer(r: &mut io::BufRead) -> Result<H256, io::Error> {
pub fn keccak_pipe(r: &mut io::BufRead, w: &mut io::Write) -> Result<H256, io::Error> {
let mut output = [0u8; 32];
let mut input = [0u8; 1024];
let mut keccak = Keccak::new_keccak256();
@@ -64,12 +64,17 @@ pub fn keccak_buffer(r: &mut io::BufRead) -> Result<H256, io::Error> {
break;
}
keccak.update(&input[0..some]);
w.write_all(&input[0..some])?;
}
keccak.finalize(&mut output);
Ok(output.into())
}
pub fn keccak_buffer(r: &mut io::BufRead) -> Result<H256, io::Error> {
keccak_pipe(r, &mut io::sink())
}
#[cfg(test)]
mod tests {
extern crate tempdir;