refactor(nochatreports): simplify crypt macro
This commit is contained in:
parent
c1268a8970
commit
884081d414
@ -1,41 +1,25 @@
|
|||||||
macro_rules! crypt_with {
|
|
||||||
($op:ident, $encoding:expr, $key:expr, $text:expr, $algo:ident) => {
|
|
||||||
match $encoding {
|
|
||||||
1 => $algo::<Base64Encoding>::$op($text, $key),
|
|
||||||
2 => $algo::<Base64rEncoding>::$op($text, $key),
|
|
||||||
_ => $algo::<NewBase64rEncoding>::$op($text, $key),
|
|
||||||
}
|
|
||||||
.map_err(|error| Error::external(error.to_string()))?
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! crypt {
|
macro_rules! crypt {
|
||||||
($op:ident, $encoding:expr, $options:expr, $text:expr) => {
|
($op:ident, $options:expr, $text:expr) => {{
|
||||||
|
macro_rules! crypt_with {
|
||||||
|
($algo:ident) => {{
|
||||||
|
let encoding = $options.get("encoding").unwrap_or_default();
|
||||||
|
let key = &$options.get::<UserDataRef<AesKey>>("$key")?.inner;
|
||||||
|
match encoding {
|
||||||
|
1 => $algo::<Base64Encoding>::$op($text, &key),
|
||||||
|
2 => $algo::<Base64rEncoding>::$op($text, &key),
|
||||||
|
_ => $algo::<NewBase64rEncoding>::$op($text, &key),
|
||||||
|
}
|
||||||
|
.map_err(|error| Error::external(error.to_string()))?
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
match $options.get("encryption").unwrap_or_default() {
|
match $options.get("encryption").unwrap_or_default() {
|
||||||
1 => CaesarEncryption::$op(&$text, &$options.get("key")?)
|
1 => CaesarEncryption::$op(&$text, &$options.get("key")?)
|
||||||
.map_err(|error| Error::external(error.to_string()))?,
|
.map_err(|error| Error::external(error.to_string()))?,
|
||||||
2 => crypt_with!(
|
2 => crypt_with!(EcbEncryption),
|
||||||
$op,
|
3 => crypt_with!(GcmEncryption),
|
||||||
$encoding,
|
_ => crypt_with!(Cfb8Encryption),
|
||||||
&$options.get::<UserDataRef<AesKey>>("key")?.inner,
|
|
||||||
&$text,
|
|
||||||
EcbEncryption
|
|
||||||
),
|
|
||||||
3 => crypt_with!(
|
|
||||||
$op,
|
|
||||||
$encoding,
|
|
||||||
&$options.get::<UserDataRef<AesKey>>("key")?.inner,
|
|
||||||
&$text,
|
|
||||||
GcmEncryption
|
|
||||||
),
|
|
||||||
_ => crypt_with!(
|
|
||||||
$op,
|
|
||||||
$encoding,
|
|
||||||
&$options.get::<UserDataRef<AesKey>>("key")?.inner,
|
|
||||||
&$text,
|
|
||||||
Cfb8Encryption
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -42,24 +42,14 @@ pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
|
|||||||
globals.set(
|
globals.set(
|
||||||
"ncr_encrypt",
|
"ncr_encrypt",
|
||||||
lua.create_function(|_, (options, plaintext): (Table, String)| {
|
lua.create_function(|_, (options, plaintext): (Table, String)| {
|
||||||
Ok(crypt!(
|
Ok(crypt!(encrypt, options, &plaintext))
|
||||||
encrypt,
|
|
||||||
options.get("encoding").unwrap_or_default(),
|
|
||||||
options,
|
|
||||||
plaintext
|
|
||||||
))
|
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
globals.set(
|
globals.set(
|
||||||
"ncr_decrypt",
|
"ncr_decrypt",
|
||||||
lua.create_function(|_, (options, ciphertext): (Table, String)| {
|
lua.create_function(|_, (options, ciphertext): (Table, String)| {
|
||||||
Ok(crypt!(
|
Ok(crypt!(decrypt, options, &ciphertext))
|
||||||
decrypt,
|
|
||||||
options.get("encoding").unwrap_or_default(),
|
|
||||||
options,
|
|
||||||
ciphertext
|
|
||||||
))
|
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user