-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated macros, now the naming of macros is unified and the log activation is now a proc macro
- Loading branch information
Showing
27 changed files
with
1,334 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "crypt_guard_proc" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "CryptGuardProc is the proc macro crate related to CryptGuardLib, which is a comprehensive Rust library designed for strong encryption and decryption, incorporating post-quantum cryptography to safeguard against quantum threats. It's geared towards developers who need to embed advanced cryptographic capabilities in their Rust applications." | ||
license = "MIT" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
# crypt_guard = { path = "../"} | ||
proc-macro2 = "1.0.81" | ||
quote = "1.0.36" | ||
syn = { version = "2.0.60", features = ["full", "extra-traits", "fold"] } | ||
hex = "0.4.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream}; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn, Meta, Lit, LitStr, Expr}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn activate_log(args: TokenStream, input: TokenStream) -> TokenStream { | ||
let log_file = parse_macro_input!(args as LitStr); | ||
let input_fn = parse_macro_input!(input as ItemFn); | ||
|
||
let output = quote! { | ||
#input_fn | ||
|
||
fn initialize_logger() { | ||
crypt_guard::initialize_logger(std::path::PathBuf::from(#log_file)); | ||
} | ||
}; | ||
|
||
TokenStream::from(output) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn ConcatKey(input: TokenStream) -> TokenStream { | ||
let inputs = parse_macro_input!(input as Expr); | ||
|
||
let output = quote! { | ||
{ | ||
let key = hex::encode(#inputs.0); | ||
let cipher = hex::encode(#inputs.1); | ||
format!("{}${}", key, cipher) | ||
} | ||
}; | ||
|
||
TokenStream::from(output) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn SplitKey(input: TokenStream) -> TokenStream { | ||
let expr = parse_macro_input!(input as Expr); | ||
|
||
let output = quote! { | ||
{ | ||
let parts: Vec<&str> = #expr.split('$').collect(); | ||
if parts.len() != 2 { | ||
Err(hex::FromHexError::OddLength) | ||
} else { | ||
match (hex::decode(parts[0]), hex::decode(parts[1])) { | ||
(Ok(key), Ok(cipher)) => Ok((key, cipher)), | ||
(Err(e), _) | (_, Err(e)) => Err(e), | ||
} | ||
} | ||
} | ||
}; | ||
|
||
TokenStream::from(output) | ||
} | ||
|
||
struct LogActivityInput { | ||
process: Expr, | ||
detail: Expr, | ||
} | ||
|
||
impl syn::parse::Parse for LogActivityInput { | ||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { | ||
let process: Expr = input.parse()?; | ||
input.parse::<syn::Token![,]>()?; | ||
let detail: Expr = input.parse()?; | ||
|
||
Ok(LogActivityInput { process, detail }) | ||
} | ||
} | ||
|
||
#[proc_macro] | ||
pub fn log_activity(input: TokenStream) -> TokenStream { | ||
let LogActivityInput { process, detail } = parse_macro_input!(input as LogActivityInput); | ||
|
||
let output = quote! { | ||
match LOGGER.lock() { | ||
Ok(mut logger) => { | ||
let _ = logger.append_log(&format!("{}", #process), &format!("{}", #detail)); | ||
}, | ||
Err(e) => eprintln!("Logger lock error: {}", e), | ||
} | ||
}; | ||
|
||
TokenStream::from(output) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn write_log(_input: TokenStream) -> TokenStream { | ||
let output = quote! { | ||
{ | ||
let mut logger = LOGGER.lock().expect("Logger lock failed"); | ||
if let Err(e) = logger.write_log_file() { | ||
eprintln!("Failed to write log file: {:?}", e); | ||
} | ||
logger.log.clear(); | ||
} | ||
}; | ||
|
||
TokenStream::from(output) | ||
} |
Oops, something went wrong.