diff --git a/src/digest_engine.rs b/src/digest_engine.rs index c8c7b98..5010298 100644 --- a/src/digest_engine.rs +++ b/src/digest_engine.rs @@ -32,6 +32,9 @@ pub trait DigestEngine { } fn create(&self) -> Result>; fn finalize(&mut self) -> io::Result; + + // Update the digest with new data + fn update(&mut self, data: &[u8]); } lazy_static! { diff --git a/src/digest_engine/none.rs b/src/digest_engine/none.rs index 0cdcf8d..99c9dab 100644 --- a/src/digest_engine/none.rs +++ b/src/digest_engine/none.rs @@ -19,4 +19,6 @@ impl DigestEngine for DigestEngineNone { fn finalize(&mut self) -> io::Result { Ok("none".to_string()) } + + fn update(&mut self, _data: &[u8]) {} } diff --git a/src/digest_engine/program.rs b/src/digest_engine/program.rs index 0de559e..042cace 100644 --- a/src/digest_engine/program.rs +++ b/src/digest_engine/program.rs @@ -16,6 +16,7 @@ pub struct DigestEngineProgram { pub program: String, pub args: Vec, pub supported: bool, + pub stdin: Option>, } impl DigestEngineProgram { @@ -26,6 +27,7 @@ impl DigestEngineProgram { program: program_path.unwrap_or(program.to_string()), args: args.iter().map(|s| s.to_string()).collect(), supported, + stdin: None, } } } @@ -34,6 +36,7 @@ impl DigestEngine for DigestEngineProgram { fn is_supported(&self) -> bool { self.supported } + fn create(&self) -> Result> { debug!("DIGEST: Writting using {:?}", *self); @@ -45,7 +48,7 @@ impl DigestEngine for DigestEngineProgram { program, args ); - let process = Command::new(program.clone()) + let mut process = Command::new(program.clone()) .args(args.clone()) .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -56,12 +59,26 @@ impl DigestEngine for DigestEngineProgram { args ))?; - Ok(Box::new(process.stdin.unwrap())) + let stdin = process.stdin.unwrap(); + let stdin = tokio::io::BufWriter::new(stdin); + + Ok(Box::new(Self { + program: program.clone(), + args: args.clone(), + stdin: Some(stdin), + supported: self.supported, + })) } fn finalize(&mut self) -> io::Result { Ok("program".to_string()) } + + fn update(&mut self, data: &[u8]) { + if let Some(stdin) = &mut self.stdin { + let _ = stdin.write(data); + } + } } fn get_program_path(program: &str) -> Result { diff --git a/src/digest_engine/sha2.rs b/src/digest_engine/sha2.rs index 7b8d8f9..14737b2 100644 --- a/src/digest_engine/sha2.rs +++ b/src/digest_engine/sha2.rs @@ -31,4 +31,8 @@ impl DigestEngine for DigestEngineSha256 { let result = self.hasher.clone().finalize(); Ok(format!("{:x}", result)) } + + fn update(&mut self, data: &[u8]) { + self.hasher.update(data); + } }