#![warn(
missing_docs,
clippy::missing_docs_in_private_items,
clippy::missing_errors_doc,
clippy::missing_panics_doc
)]
mod api_library_input_struct;
pub mod checkers;
mod cli_input_parser;
pub mod cli_pretty_printing;
pub mod config;
pub mod decoders;
mod filtration_system;
mod searchers;
mod storage;
mod timer;
use checkers::{
athena::Athena,
checker_result::CheckResult,
checker_type::{Check, Checker},
};
use log::debug;
use crate::{config::Config, decoders::interface::Decoder};
use self::decoders::crack_results::CrackResult;
pub fn perform_cracking(text: &str, config: Config) -> Option<DecoderResult> {
config::set_global_config(config);
let initial_check_for_plaintext = check_if_input_text_is_plaintext(text);
if initial_check_for_plaintext.is_identified {
debug!(
"The input text provided to the program {} is the plaintext. Returning early.",
text
);
cli_pretty_printing::return_early_because_input_text_is_plaintext();
let mut crack_result = CrackResult::new(&Decoder::default(), (&text).to_string());
crack_result.checker_name = initial_check_for_plaintext.checker_name;
let output = DecoderResult {
text: vec![(&text).to_string()],
path: vec![crack_result],
};
return Some(output);
}
searchers::search_for_plaintext(text)
}
fn check_if_input_text_is_plaintext(text: &str) -> CheckResult {
let athena_checker = Checker::<Athena>::new();
athena_checker.check(text)
}
#[derive(Debug)]
pub struct DecoderResult {
pub text: Vec<String>,
pub path: Vec<CrackResult>,
}
impl Default for DecoderResult {
fn default() -> Self {
DecoderResult {
text: vec!["Default".to_string()],
path: vec![CrackResult::new(&Decoder::default(), "Default".to_string())],
}
}
}
impl DecoderResult {
fn _new(text: &str) -> Self {
DecoderResult {
text: vec![text.to_string()],
path: vec![CrackResult::new(&Decoder::default(), "Default".to_string())],
}
}
}
#[cfg(test)]
mod tests {
use super::perform_cracking;
use crate::config::Config;
#[test]
fn test_perform_cracking_returns() {
let config = Config::default();
perform_cracking("SGVscCBJIG5lZWQgc29tZWJvZHkh", config);
}
#[test]
fn test_perform_cracking_returns_successful() {
let config = Config::default();
let result = perform_cracking("b2xsZWg=", config);
assert!(result.is_some());
assert!(result.unwrap().text[0] == "hello");
}
#[test]
fn test_perform_cracking_returns_failure() {
let config = Config::default();
let result = perform_cracking("", config);
assert!(result.is_none());
}
#[test]
fn test_perform_cracking_returns_successful_base64_reverse() {
let config = Config::default();
let result = perform_cracking("aGVsbG8gdGhlcmUgZ2VuZXJhbA==", config);
assert!(result.is_some());
assert!(result.unwrap().text[0] == "hello there general")
}
#[test]
fn test_early_exit_if_input_is_plaintext() {
let config = Config::default();
let result = perform_cracking("192.168.0.1", config);
assert!(result.unwrap().path.len() == 1);
}
#[test]
fn test_successfully_decode_caesar() {
let config = Config::default();
let result = perform_cracking("Ebgngr zr 13 cynprf!", config);
assert!(result.unwrap().text[0] == "Rotate me 13 places!");
}
#[test]
fn test_successfully_inputted_plaintext() {
let config = Config::default();
let result = perform_cracking("Hello, World!", config);
let res_unwrapped = result.unwrap();
assert!(&res_unwrapped.text[0] == "Hello, World!");
assert!(res_unwrapped.path[0].decoder == "Default decoder");
}
}