feat: add support for NoChatReports encryption
This commit is contained in:
@@ -4,6 +4,7 @@ pub mod container;
|
||||
pub mod direction;
|
||||
pub mod events;
|
||||
pub mod logging;
|
||||
pub mod nochatreports;
|
||||
pub mod player;
|
||||
pub mod system;
|
||||
pub mod vec3;
|
||||
@@ -39,6 +40,7 @@ pub fn register_functions(
|
||||
block::register_functions(lua, globals)?;
|
||||
events::register_functions(lua, globals, event_listeners)?;
|
||||
logging::register_functions(lua, globals)?;
|
||||
nochatreports::register_functions(lua, globals)?;
|
||||
system::register_functions(lua, globals)
|
||||
}
|
||||
|
||||
|
41
src/lua/nochatreports/crypt.rs
Normal file
41
src/lua/nochatreports/crypt.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
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_rules! crypt {
|
||||
($op:ident, $encoding:expr, $options:expr, $text:expr) => {
|
||||
match $options.get("encryption").unwrap_or_default() {
|
||||
1 => CaesarEncryption::$op(&$text, &$options.get("key")?)
|
||||
.map_err(|error| Error::external(error.to_string()))?,
|
||||
2 => crypt_with!(
|
||||
$op,
|
||||
$encoding,
|
||||
&$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
|
||||
),
|
||||
}
|
||||
};
|
||||
}
|
12
src/lua/nochatreports/key.rs
Normal file
12
src/lua/nochatreports/key.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use mlua::UserData;
|
||||
|
||||
pub struct AesKey {
|
||||
pub inner: ncr::AesKey,
|
||||
}
|
||||
|
||||
impl UserData for AesKey {
|
||||
fn add_fields<F: mlua::UserDataFields<Self>>(f: &mut F) {
|
||||
f.add_field_method_get("base64", |_, this| Ok(this.inner.encode_base64()));
|
||||
f.add_field_method_get("bytes", |_, this| Ok(this.inner.as_ref().to_vec()));
|
||||
}
|
||||
}
|
81
src/lua/nochatreports/mod.rs
Normal file
81
src/lua/nochatreports/mod.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
#[macro_use]
|
||||
pub mod crypt;
|
||||
pub mod key;
|
||||
|
||||
use key::AesKey;
|
||||
use mlua::{Error, Lua, Result, Table, UserDataRef};
|
||||
use ncr::{
|
||||
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
|
||||
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
|
||||
utils::{prepend_header, trim_header},
|
||||
};
|
||||
|
||||
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
|
||||
globals.set(
|
||||
"ncr_aes_key_from_passphrase",
|
||||
lua.create_function(|_, passphrase: Vec<u8>| {
|
||||
Ok(AesKey {
|
||||
inner: ncr::AesKey::gen_from_passphrase(&passphrase),
|
||||
})
|
||||
})?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_aes_key_from_base64",
|
||||
lua.create_function(|_, base64: String| {
|
||||
Ok(AesKey {
|
||||
inner: ncr::AesKey::decode_base64(&base64)
|
||||
.map_err(|error| Error::external(error.to_string()))?,
|
||||
})
|
||||
})?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_generate_random_aes_key",
|
||||
lua.create_function(|_, (): ()| {
|
||||
Ok(AesKey {
|
||||
inner: ncr::AesKey::gen_random_key(),
|
||||
})
|
||||
})?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_encrypt",
|
||||
lua.create_function(|_, (options, plaintext): (Table, String)| {
|
||||
Ok(crypt!(
|
||||
encrypt,
|
||||
options.get("encoding").unwrap_or_default(),
|
||||
options,
|
||||
plaintext
|
||||
))
|
||||
})?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_decrypt",
|
||||
lua.create_function(|_, (options, ciphertext): (Table, String)| {
|
||||
Ok(crypt!(
|
||||
decrypt,
|
||||
options.get("encoding").unwrap_or_default(),
|
||||
options,
|
||||
ciphertext
|
||||
))
|
||||
})?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_prepend_header",
|
||||
lua.create_function(|_, text: String| Ok(prepend_header(&text)))?,
|
||||
)?;
|
||||
|
||||
globals.set(
|
||||
"ncr_trim_header",
|
||||
lua.create_function(|_, text: String| {
|
||||
Ok(trim_header(&text)
|
||||
.map_err(|error| Error::external(error.to_string()))?
|
||||
.to_owned())
|
||||
})?,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user