Skip to content

Commit

Permalink
Remove From<&str, &str, ContextType> impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 7, 2024
1 parent e200214 commit fea6024
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
47 changes: 22 additions & 25 deletions rust/src/nasl/builtin/cryptographic/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sha2::{Sha256, Sha384, Sha512};

use crate::nasl::prelude::*;

fn hmac<D>(register: &Register) -> Result<NaslValue, FunctionErrorKind>
fn hmac<D>(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind>
where
D: CoreProxy,
D::Core: HashMarker
Expand All @@ -31,16 +31,6 @@ where
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
let key = match register.named("key") {
Some(ContextType::Value(NaslValue::String(x))) => x,
Some(ContextType::Value(NaslValue::Null)) => return Ok(NaslValue::Null),
x => return Err(("key", "string", x).into()),
};
let data = match register.named("data") {
Some(ContextType::Value(NaslValue::String(x))) => x,
Some(ContextType::Value(NaslValue::Null)) => return Ok(NaslValue::Null),
x => return Err(("data", "string", x).into()),
};
let mut hmac = match Hmac::<D>::new_from_slice(key.as_bytes()) {
Ok(x) => x,
Err(InvalidLength) => {
Expand All @@ -57,38 +47,45 @@ where
}

/// NASL function to get HMAC MD2 string
pub fn hmac_md2(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Md2>(register)
#[nasl_function(named(key, data))]
pub fn hmac_md2(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Md2>(key, data)
}

/// NASL function to get HMAC MD5 string
pub fn hmac_md5(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Md5>(register)
#[nasl_function(named(key, data))]
pub fn hmac_md5(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Md5>(key, data)
}

/// NASL function to get HMAC RIPEMD160 string
pub fn hmac_ripemd160(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Ripemd160>(register)
#[nasl_function(named(key, data))]
pub fn hmac_ripemd160(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Ripemd160>(key, data)
}

/// NASL function to get HMAC SHA1 string
pub fn hmac_sha1(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha1>(register)
#[nasl_function(named(key, data))]
pub fn hmac_sha1(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha1>(key, data)
}

/// NASL function to get HMAC SHA256 string
pub fn hmac_sha256(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha256>(register)
#[nasl_function(named(key, data))]
pub fn hmac_sha256(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha256>(key, data)
}

/// NASL function to get HMAC SHA384 string
pub fn hmac_sha384(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha384>(register)
#[nasl_function(named(key, data))]
pub fn hmac_sha384(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha384>(key, data)
}

/// NASL function to get HMAC SHA512 string
pub fn hmac_sha512(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha512>(register)
#[nasl_function(named(key, data))]
pub fn hmac_sha512(key: &str, data: &str) -> Result<NaslValue, FunctionErrorKind> {
hmac::<Sha512>(key, data)
}

pub struct HmacFns;
Expand Down
14 changes: 0 additions & 14 deletions rust/src/nasl/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::nasl::prelude::NaslValue;

use crate::storage::StorageError;

use super::ContextType;

#[derive(Debug, Clone, Error)]
/// Descriptive kind of error that can occur while calling a function
pub enum FunctionErrorKind {
Expand Down Expand Up @@ -147,15 +145,3 @@ impl From<(&str, &str, Option<&NaslValue>)> for FunctionErrorKind {
}
}
}

impl From<(&str, &str, Option<&ContextType>)> for FunctionErrorKind {
fn from(value: (&str, &str, Option<&ContextType>)) -> Self {
match value {
(key, expected, Some(ContextType::Value(x))) => (key, expected, x).into(),
(key, expected, Some(ContextType::Function(_, _))) => {
ArgumentError::wrong_argument(key, expected, "function").into()
}
(key, expected, None) => ArgumentError::wrong_argument(key, expected, "NULL").into(),
}
}
}

0 comments on commit fea6024

Please sign in to comment.