Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early if the input is the plaintext #115

Merged
merged 6 commits into from
Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ mod storage;
/// Timer for internal use
mod timer;

use checkers::{
athena::Athena,
checker_type::{Check, Checker},
};
use log::debug;

use crate::config::Config;

use self::decoders::crack_results::CrackResult;
Expand All @@ -45,6 +51,15 @@ use self::decoders::crack_results::CrackResult;
/// assert!(true)
/// ```
pub fn perform_cracking(text: &str, config: Config) -> Option<DecoderResult> {
if check_if_input_text_is_plaintext(text) {
debug!(
"The input text provided to the program {} is the plaintext. Returning early.",
text
);
return_early_because_input_text_is_plaintext();
return None;
}

// Build a new search tree
// This starts us with a node with no parents
// let search_tree = searchers::Tree::new(text.to_string());
Expand All @@ -54,6 +69,18 @@ pub fn perform_cracking(text: &str, config: Config) -> Option<DecoderResult> {
searchers::search_for_plaintext(text)
}

/// Checks if the given input is plaintext or not
/// Used at the start of the program to not waste CPU cycles
fn check_if_input_text_is_plaintext(text: &str) -> bool {
let athena_checker = Checker::<Athena>::new();
athena_checker.check(text).is_identified
}

/// A nice function to print when the input text is the plaintext
fn return_early_because_input_text_is_plaintext() {
println!("Your input text is the plaintext 🥳")
SkeletalDemise marked this conversation as resolved.
Show resolved Hide resolved
}

/// DecoderResult is the result of decoders
#[derive(Debug)]
pub struct DecoderResult {
Expand Down Expand Up @@ -102,4 +129,12 @@ mod tests {
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);
// We return None since the input is the plaintext
assert!(result.is_none());
}
}