fix: implement Write trait for digest engines
This commit is contained in:
@@ -14,11 +14,31 @@ impl DigestEngineNone {
|
||||
|
||||
impl DigestEngine for DigestEngineNone {
|
||||
fn create(&self) -> Result<Box<dyn Write>> {
|
||||
Ok(Box::new(Self::new()))
|
||||
Ok(Box::new(DummyWriter::new()))
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> io::Result<String> {
|
||||
Ok("none".to_string())
|
||||
}
|
||||
|
||||
fn update(&mut self, _data: &[u8]) {}
|
||||
}
|
||||
|
||||
// Dummy writer that implements Write for the none digest engine
|
||||
struct DummyWriter;
|
||||
|
||||
impl DummyWriter {
|
||||
fn new() -> Self {
|
||||
DummyWriter
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for DummyWriter {
|
||||
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,14 +59,9 @@ impl DigestEngine for DigestEngineProgram {
|
||||
args
|
||||
))?;
|
||||
|
||||
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,
|
||||
Ok(Box::new(ProgramWriter {
|
||||
stdin: process.stdin.take().unwrap(),
|
||||
process: Some(process),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -75,9 +70,7 @@ impl DigestEngine for DigestEngineProgram {
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) {
|
||||
if let Some(stdin) = &mut self.stdin {
|
||||
let _ = stdin.write(data);
|
||||
}
|
||||
// This is handled by the ProgramWriter implementation
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,37 @@ impl DigestEngineSha256 {
|
||||
}
|
||||
}
|
||||
|
||||
// Create a writer that updates the hasher
|
||||
fn create_writer(&self) -> Sha256Writer {
|
||||
Sha256Writer::new(self.hasher.clone())
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper that implements Write for the Sha256 hasher
|
||||
struct Sha256Writer {
|
||||
hasher: Sha256,
|
||||
}
|
||||
|
||||
impl Sha256Writer {
|
||||
fn new(hasher: Sha256) -> Self {
|
||||
Sha256Writer { hasher }
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Sha256Writer {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.hasher.update(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestEngine for DigestEngineSha256 {
|
||||
fn create(&self) -> Result<Box<dyn Write>> {
|
||||
Box::new(Self::new())
|
||||
Ok(Box::new(self.create_writer()))
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> io::Result<String> {
|
||||
|
||||
Reference in New Issue
Block a user