feat: create digest engine with Sha256/Md5/None and integrate with save mode

This commit is contained in:
Andrew Phillips (aider)
2025-05-12 18:21:34 -03:00
parent f6b6bc5768
commit 41060c3029
5 changed files with 130 additions and 0 deletions

25
src/digest_engine/none.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::io;
use log::*;
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DigestEngineNone {}
impl DigestEngineNone {
pub fn new() -> DigestEngineNone {
DigestEngineNone {}
}
}
impl DigestEngine for DigestEngineNone {
fn create(&self) -> Box<dyn DigestEngine> {
Box::new(Self::new())
}
fn update(&mut self, _data: &[u8]) -> io::Result<()> {
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
Ok("none".to_string())
}
}

View File

@@ -0,0 +1,28 @@
use std::io;
use std::process::{Command, Stdio};
use log::*;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DigestEngineProgram {
program: String,
}
impl DigestEngineProgram {
pub fn new(program: &str) -> DigestEngineProgram {
DigestEngineProgram { program: program.to_string() }
}
}
impl DigestEngine for DigestEngineProgram {
fn create(&self) -> Box<dyn DigestEngine> {
Box::new(Self::new(&self.program))
}
fn update(&mut self, _data: &[u8]) -> io::Result<()> {
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
Ok("program".to_string())
}
}

30
src/digest_engine/sha2.rs Normal file
View File

@@ -0,0 +1,30 @@
use std::io;
use log::*;
use sha2::{Digest, Sha256};
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DigestEngineSha256 {
hasher: Sha256,
}
impl DigestEngineSha256 {
pub fn new() -> DigestEngineSha256 {
DigestEngineSha256 { hasher: Sha256::new() }
}
}
impl DigestEngine for DigestEngineSha256 {
fn create(&self) -> Box<dyn DigestEngine> {
Box::new(Self::new())
}
fn update(&mut self, data: &[u8]) -> io::Result<()> {
self.hasher.update(data);
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
let result = self.hasher.finalize();
Ok(format!("{:x}", result))
}
}