feat: unify digest plugin types and always compute all hashes
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -2,53 +2,27 @@ use sha2::{Digest, Sha256, Sha512};
|
|||||||
use md5::Md5;
|
use md5::Md5;
|
||||||
use crate::meta_plugin::MetaPlugin;
|
use crate::meta_plugin::MetaPlugin;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
enum HashMethod {
|
|
||||||
Md5,
|
|
||||||
Sha256,
|
|
||||||
Sha512,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for HashMethod {
|
|
||||||
fn default() -> Self {
|
|
||||||
HashMethod::Sha256
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HashMethod {
|
|
||||||
fn from_str(s: &str) -> Option<Self> {
|
|
||||||
match s {
|
|
||||||
"md5" => Some(HashMethod::Md5),
|
|
||||||
"sha256" => Some(HashMethod::Sha256),
|
|
||||||
"sha512" => Some(HashMethod::Sha512),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DigestMetaPlugin {
|
pub struct DigestMetaPlugin {
|
||||||
md5_hasher: Option<Md5>,
|
md5_hasher: Md5,
|
||||||
sha256_hasher: Option<Sha256>,
|
sha256_hasher: Sha256,
|
||||||
sha512_hasher: Option<Sha512>,
|
sha512_hasher: Sha512,
|
||||||
is_finalized: bool,
|
is_finalized: bool,
|
||||||
meta_name: String,
|
meta_name: String,
|
||||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
||||||
options: std::collections::HashMap<String, serde_yaml::Value>,
|
options: std::collections::HashMap<String, serde_yaml::Value>,
|
||||||
methods: Vec<HashMethod>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DigestMetaPlugin {
|
impl Default for DigestMetaPlugin {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
md5_hasher: None,
|
md5_hasher: Md5::new(),
|
||||||
sha256_hasher: None,
|
sha256_hasher: Sha256::new(),
|
||||||
sha512_hasher: None,
|
sha512_hasher: Sha512::new(),
|
||||||
is_finalized: false,
|
is_finalized: false,
|
||||||
meta_name: "digest".to_string(),
|
meta_name: "digest".to_string(),
|
||||||
outputs: std::collections::HashMap::new(),
|
outputs: std::collections::HashMap::new(),
|
||||||
options: std::collections::HashMap::new(),
|
options: std::collections::HashMap::new(),
|
||||||
methods: vec![HashMethod::Sha256],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,24 +58,6 @@ impl DigestMetaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure methods based on options
|
|
||||||
if let Some(method_value) = plugin.options.get("method") {
|
|
||||||
if let Some(method_str) = method_value.as_str() {
|
|
||||||
if let Some(method) = HashMethod::from_str(method_str) {
|
|
||||||
plugin.methods = vec![method];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize hashers based on selected methods
|
|
||||||
for method in &plugin.methods {
|
|
||||||
match method {
|
|
||||||
HashMethod::Md5 => plugin.md5_hasher = Some(Md5::new()),
|
|
||||||
HashMethod::Sha256 => plugin.sha256_hasher = Some(Sha256::new()),
|
|
||||||
HashMethod::Sha512 => plugin.sha512_hasher = Some(Sha512::new()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
plugin
|
plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,49 +92,35 @@ impl MetaPlugin for DigestMetaPlugin {
|
|||||||
|
|
||||||
let mut metadata = Vec::new();
|
let mut metadata = Vec::new();
|
||||||
|
|
||||||
// Process each method
|
// Always compute all three hashes
|
||||||
for method in &self.methods {
|
let md5_result = self.md5_hasher.clone().finalize();
|
||||||
match method {
|
let md5_hex = format!("{:x}", md5_result);
|
||||||
HashMethod::Md5 => {
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
if let Some(hasher) = self.md5_hasher.take() {
|
"digest_md5",
|
||||||
let hash_result = hasher.finalize();
|
md5_hex,
|
||||||
let hex_string = format!("{:x}", hash_result);
|
&self.outputs
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
) {
|
||||||
"digest_md5",
|
metadata.push(meta_data);
|
||||||
hex_string,
|
}
|
||||||
&self.outputs
|
|
||||||
) {
|
let sha256_result = self.sha256_hasher.clone().finalize_reset();
|
||||||
metadata.push(meta_data);
|
let sha256_hex = format!("{:x}", sha256_result);
|
||||||
}
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
}
|
"digest_sha256",
|
||||||
}
|
sha256_hex,
|
||||||
HashMethod::Sha256 => {
|
&self.outputs
|
||||||
if let Some(hasher) = self.sha256_hasher.take() {
|
) {
|
||||||
let hash_result = hasher.finalize_reset();
|
metadata.push(meta_data);
|
||||||
let hex_string = format!("{:x}", hash_result);
|
}
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
|
||||||
"digest_sha256",
|
let sha512_result = self.sha512_hasher.clone().finalize_reset();
|
||||||
hex_string,
|
let sha512_hex = format!("{:x}", sha512_result);
|
||||||
&self.outputs
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
) {
|
"digest_sha512",
|
||||||
metadata.push(meta_data);
|
sha512_hex,
|
||||||
}
|
&self.outputs
|
||||||
}
|
) {
|
||||||
}
|
metadata.push(meta_data);
|
||||||
HashMethod::Sha512 => {
|
|
||||||
if let Some(hasher) = self.sha512_hasher.take() {
|
|
||||||
let hash_result = hasher.finalize_reset();
|
|
||||||
let hex_string = format!("{:x}", hash_result);
|
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
|
||||||
"digest_sha512",
|
|
||||||
hex_string,
|
|
||||||
&self.outputs
|
|
||||||
) {
|
|
||||||
metadata.push(meta_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.is_finalized = true;
|
self.is_finalized = true;
|
||||||
@@ -196,25 +138,10 @@ impl MetaPlugin for DigestMetaPlugin {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for method in &self.methods {
|
// Update all hashers
|
||||||
match method {
|
self.md5_hasher.update(data);
|
||||||
HashMethod::Md5 => {
|
self.sha256_hasher.update(data);
|
||||||
if let Some(hasher) = &mut self.md5_hasher {
|
self.sha512_hasher.update(data);
|
||||||
hasher.update(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HashMethod::Sha256 => {
|
|
||||||
if let Some(hasher) = &mut self.sha256_hasher {
|
|
||||||
hasher.update(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HashMethod::Sha512 => {
|
|
||||||
if let Some(hasher) = &mut self.sha512_hasher {
|
|
||||||
hasher.update(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
crate::meta_plugin::MetaPluginResponse {
|
crate::meta_plugin::MetaPluginResponse {
|
||||||
metadata: Vec::new(),
|
metadata: Vec::new(),
|
||||||
|
|||||||
@@ -133,8 +133,7 @@ pub enum MetaPluginType {
|
|||||||
Shell,
|
Shell,
|
||||||
ShellPid,
|
ShellPid,
|
||||||
KeepPid,
|
KeepPid,
|
||||||
DigestSha256,
|
Digest,
|
||||||
DigestMd5,
|
|
||||||
ReadTime,
|
ReadTime,
|
||||||
ReadRate,
|
ReadRate,
|
||||||
Hostname,
|
Hostname,
|
||||||
@@ -296,8 +295,7 @@ pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box<dyn MetaPlugin>
|
|||||||
MetaPluginType::Shell => Box::new(ShellMetaPlugin::new_simple()),
|
MetaPluginType::Shell => Box::new(ShellMetaPlugin::new_simple()),
|
||||||
MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new_simple()),
|
MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new_simple()),
|
||||||
MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new_simple()),
|
MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new_simple()),
|
||||||
MetaPluginType::DigestSha256 => Box::new(DigestMetaPlugin::new_simple()),
|
MetaPluginType::Digest => Box::new(DigestMetaPlugin::new_simple()),
|
||||||
MetaPluginType::DigestMd5 => Box::new(MetaPluginProgram::new_simple("md5sum", vec![], "digest_md5".to_string(), true)),
|
|
||||||
MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new_simple()),
|
MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new_simple()),
|
||||||
MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new_simple()),
|
MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new_simple()),
|
||||||
MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new_simple()),
|
MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new_simple()),
|
||||||
|
|||||||
Reference in New Issue
Block a user