Port Spam filter to Rust - part 8
This commit is contained in:
@@ -4,10 +4,9 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
|
||||
*/
|
||||
|
||||
use std::{borrow::Cow, cmp::Ordering, fmt::Display, net::IpAddr};
|
||||
use std::{borrow::Cow, cmp::Ordering, fmt::Display};
|
||||
|
||||
use hyper::StatusCode;
|
||||
use mail_auth::common::resolver::ToReverseName;
|
||||
use trc::EvalEvent;
|
||||
|
||||
use crate::Server;
|
||||
@@ -15,7 +14,6 @@ use crate::Server;
|
||||
use super::{
|
||||
functions::{ResolveVariable, FUNCTIONS},
|
||||
if_block::IfBlock,
|
||||
tokenizer::TokenMap,
|
||||
BinaryOperator, Constant, Expression, ExpressionItem, UnaryOperator, Variable,
|
||||
};
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ pub(crate) const FUNCTIONS: &[(&str, fn(Vec<Variable>) -> Variable, u32)] = &[
|
||||
("rsplit_once", text::fn_rsplit_once, 2),
|
||||
("split_n", text::fn_split_n, 3),
|
||||
("split_words", text::fn_split_words, 1),
|
||||
("hash", text::fn_hash, 2),
|
||||
];
|
||||
|
||||
pub const F_IS_LOCAL_DOMAIN: u32 = 0;
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use sha1::Sha1;
|
||||
use sha2::{Sha256, Sha512};
|
||||
|
||||
use crate::expr::Variable;
|
||||
|
||||
pub(crate) fn fn_trim(mut v: Vec<Variable>) -> Variable {
|
||||
@@ -312,3 +315,30 @@ pub(crate) fn fn_rsplit_once(v: Vec<Variable>) -> Variable {
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fn_hash(v: Vec<Variable>) -> Variable {
|
||||
use sha1::Digest;
|
||||
let mut v = v.into_iter();
|
||||
let value = v.next().unwrap().into_string();
|
||||
let algo = v.next().unwrap().into_string();
|
||||
|
||||
match algo.as_ref() {
|
||||
"md5" => format!("{:x}", md5::compute(value.as_bytes())).into(),
|
||||
"sha1" => {
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.update(value.as_bytes());
|
||||
format!("{:x}", hasher.finalize()).into()
|
||||
}
|
||||
"sha256" => {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(value.as_bytes());
|
||||
format!("{:x}", hasher.finalize()).into()
|
||||
}
|
||||
"sha512" => {
|
||||
let mut hasher = Sha512::new();
|
||||
hasher.update(value.as_bytes());
|
||||
format!("{:x}", hasher.finalize()).into()
|
||||
}
|
||||
_ => Variable::default(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user