Skip to content

Commit

Permalink
Remove From<(&str, &str, &NaslValue)> impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 7, 2024
1 parent 0fd0f0e commit ac228a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
64 changes: 31 additions & 33 deletions rust/src/nasl/builtin/cryptographic/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<D: Digest>(register: &Register) -> Result<NaslValue, FunctionErrorKind>
fn nasl_hash<D: Digest>(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind>
where
D::OutputSize: std::ops::Add,
<D::OutputSize as std::ops::Add>::Output: digest::generic_array::ArrayLength<u8>,
{
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<NaslValue, FunctionErrorKind> {
nasl_hash::<Md2>(register)
#[nasl_function]
pub fn hash_md2(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Md2>(data)
}

/// NASL function to get MD4 hash
pub fn hash_md4(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Md4>(register)
#[nasl_function]
pub fn hash_md4(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Md4>(data)
}

/// NASL function to get MD5 hash
pub fn hash_md5(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Md5>(register)
#[nasl_function]
pub fn hash_md5(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Md5>(data)
}

/// NASL function to get SHA1 hash
pub fn hash_sha1(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha1>(register)
#[nasl_function]
pub fn hash_sha1(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha1>(data)
}

/// NASL function to get SHA256 hash
pub fn hash_sha256(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha256>(register)
#[nasl_function]
pub fn hash_sha256(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha256>(data)
}

/// NASL function to get SHA512 hash
pub fn hash_sha512(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha512>(register)
#[nasl_function]
pub fn hash_sha512(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Sha512>(data)
}

/// NASL function to get RIPemd160 hash
pub fn hash_ripemd160(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Ripemd160>(register)
#[nasl_function]
pub fn hash_ripemd160(data: Option<StringOrData>) -> Result<NaslValue, FunctionErrorKind> {
nasl_hash::<Ripemd160>(data)
}

pub struct Hash;
Expand Down
17 changes: 0 additions & 17 deletions rust/src/nasl/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}

0 comments on commit ac228a3

Please sign in to comment.