refactor(commands): directly use native encryption function
This commit is contained in:
parent
ca8c9d4d1c
commit
2cf4265732
@ -1,11 +1,15 @@
|
|||||||
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
|
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
|
||||||
use futures::lock::Mutex;
|
use futures::lock::Mutex;
|
||||||
use mlua::{Function, Table};
|
use mlua::{Error, Result, Table, UserDataRef};
|
||||||
use ncr::utils::prepend_header;
|
use ncr::{
|
||||||
|
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
|
||||||
|
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
|
||||||
|
utils::prepend_header,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
State,
|
State, crypt,
|
||||||
lua::{eval, exec, reload},
|
lua::{eval, exec, nochatreports::key::AesKey, reload},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Ctx = CommandContext<Mutex<CommandSource>>;
|
pub type Ctx = CommandContext<Mutex<CommandSource>>;
|
||||||
@ -19,18 +23,20 @@ pub struct CommandSource {
|
|||||||
|
|
||||||
impl CommandSource {
|
impl CommandSource {
|
||||||
pub fn reply(&self, message: &str) {
|
pub fn reply(&self, message: &str) {
|
||||||
let ncr_data = self
|
fn encrypt(options: &Table, plaintext: &str) -> Result<String> {
|
||||||
.ncr_options
|
Ok(crypt!(encrypt, options, &prepend_header(plaintext)))
|
||||||
.as_ref()
|
}
|
||||||
.zip(self.state.lua.globals().get::<Function>("ncr_encrypt").ok());
|
|
||||||
for mut chunk in message
|
for mut chunk in message
|
||||||
.chars()
|
.chars()
|
||||||
.collect::<Vec<char>>()
|
.collect::<Vec<char>>()
|
||||||
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
|
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
|
||||||
.map(|chars| chars.iter().collect::<String>())
|
.map(|chars| chars.iter().collect::<String>())
|
||||||
{
|
{
|
||||||
if let Some((options, ref encrypt)) = ncr_data
|
if let Some(ciphertext) = self
|
||||||
&& let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
|
.ncr_options
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|options| encrypt(options, &chunk).ok())
|
||||||
{
|
{
|
||||||
chunk = ciphertext;
|
chunk = ciphertext;
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,8 @@ macro_rules! crypt {
|
|||||||
($op:ident, $options:expr, $text:expr) => {{
|
($op:ident, $options:expr, $text:expr) => {{
|
||||||
macro_rules! crypt_with {
|
macro_rules! crypt_with {
|
||||||
($algo:ident) => {{
|
($algo:ident) => {{
|
||||||
let encoding = $options.get("encoding").unwrap_or_default();
|
|
||||||
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
|
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
|
||||||
match encoding {
|
match $options.get("encoding").unwrap_or_default() {
|
||||||
1 => $algo::<Base64Encoding>::$op($text, &key),
|
1 => $algo::<Base64Encoding>::$op($text, &key),
|
||||||
2 => $algo::<Base64rEncoding>::$op($text, &key),
|
2 => $algo::<Base64rEncoding>::$op($text, &key),
|
||||||
_ => $algo::<NewBase64rEncoding>::$op($text, &key),
|
_ => $algo::<NewBase64rEncoding>::$op($text, &key),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user