From ac228a36b7dda68d436a1765b06839cf36efd5c9 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Thu, 7 Nov 2024 10:01:46 +0100 Subject: [PATCH] Remove From<(&str, &str, &NaslValue)> impl --- rust/src/nasl/builtin/cryptographic/hash.rs | 64 ++++++++++----------- rust/src/nasl/utils/error.rs | 17 ------ 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/rust/src/nasl/builtin/cryptographic/hash.rs b/rust/src/nasl/builtin/cryptographic/hash.rs index 5289e0033..5be8a996a 100644 --- a/rust/src/nasl/builtin/cryptographic/hash.rs +++ b/rust/src/nasl/builtin/cryptographic/hash.rs @@ -2,8 +2,6 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception -use crate::function_set; -use crate::nasl::utils::error::FunctionErrorKind; use digest::Digest; use md2::Md2; use md4::Md4; @@ -12,63 +10,63 @@ use ripemd::Ripemd160; use sha1::Sha1; use sha2::{Sha256, Sha512}; -use crate::nasl::syntax::NaslValue; -use crate::nasl::utils::{Context, Register}; +use crate::nasl::prelude::*; +use crate::nasl::utils::function::StringOrData; -fn nasl_hash(register: &Register) -> Result +fn nasl_hash(data: Option) -> Result where D::OutputSize: std::ops::Add, ::Output: digest::generic_array::ArrayLength, { - let positional = register.positional(); - if positional.is_empty() { - return Ok(NaslValue::Null); - }; - let data = match &positional[0] { - NaslValue::String(x) => x.as_bytes(), - NaslValue::Data(x) => x, - NaslValue::Null => return Ok(NaslValue::Null), - x => return Err(("data", "string", x).into()), - }; - - let mut hash = D::new(); - hash.update(data); - Ok(NaslValue::Data(hash.finalize().as_slice().to_vec())) + if let Some(data) = data { + let mut hash = D::new(); + hash.update(data.0.as_bytes()); + Ok(NaslValue::Data(hash.finalize().as_slice().to_vec())) + } else { + Ok(NaslValue::Null) + } } /// NASL function to get MD2 hash -pub fn hash_md2(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_md2(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get MD4 hash -pub fn hash_md4(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_md4(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get MD5 hash -pub fn hash_md5(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_md5(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get SHA1 hash -pub fn hash_sha1(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_sha1(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get SHA256 hash -pub fn hash_sha256(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_sha256(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get SHA512 hash -pub fn hash_sha512(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_sha512(data: Option) -> Result { + nasl_hash::(data) } /// NASL function to get RIPemd160 hash -pub fn hash_ripemd160(register: &Register, _: &Context) -> Result { - nasl_hash::(register) +#[nasl_function] +pub fn hash_ripemd160(data: Option) -> Result { + nasl_hash::(data) } pub struct Hash; diff --git a/rust/src/nasl/utils/error.rs b/rust/src/nasl/utils/error.rs index d699ff002..13b4dce8d 100644 --- a/rust/src/nasl/utils/error.rs +++ b/rust/src/nasl/utils/error.rs @@ -128,20 +128,3 @@ impl FunctionErrorKind { Self::Argument(ArgumentError::MissingNamed(vec![val.to_string()])) } } - -impl From<(&str, &str, &NaslValue)> for FunctionErrorKind { - fn from(value: (&str, &str, &NaslValue)) -> Self { - let (key, expected, got) = value; - let got: &str = &got.to_string(); - ArgumentError::wrong_argument(key, expected, got).into() - } -} - -impl From<(&str, &str, Option<&NaslValue>)> for FunctionErrorKind { - fn from(value: (&str, &str, Option<&NaslValue>)) -> Self { - match value { - (key, expected, Some(x)) => (key, expected, x).into(), - (key, expected, None) => ArgumentError::wrong_argument(key, expected, "NULL").into(), - } - } -}