diff --git a/Cargo.toml b/Cargo.toml index 9fd094f8..300ee3c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,10 @@ lemmeknow = { version = "0.5", default-features = false } include_dir = "0.7.2" once_cell = "1.13.0" text_io = "0.1.10" +reqwest = { version = "0.11", features = ["blocking", "json"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +syn = "1.0.40" [profile.release] lto = "fat" diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs new file mode 100644 index 00000000..06b11f5a --- /dev/null +++ b/src/decoders/hash_decoder.rs @@ -0,0 +1,132 @@ +use log::{debug, trace}; + +use std::collections::HashMap; + +use super::interface::Crack; +use super::interface::Decoder; + +use serde::Deserialize; + +pub struct HashDecoder; + +impl Crack for Decoder { + fn new() -> Decoder { + Decoder { + name: "hash", + description: "A cryptographic hash function is a hash function which takes an input (or 'message') and returns a fixed-size string of bytes. The string is called the 'hash value', 'message digest', 'digital fingerprint', 'digest' or 'checksum'.", + link: "https://en.wikipedia.org/wiki/Hash_function", + tags: vec!["hash", "decoder"], + expected_runtime: 0.8, + expected_success: 0.8, + failure_runtime: 0.8, + normalised_entropy: vec![1.0, 10.0], + popularity: 0.4, + phantom: std::marker::PhantomData, + } + } + + fn crack(&self, text: &str) -> Option { + trace!("Trying hash with text {:?}", text); + + let decoded_text = decode_hash_no_error_handling(text); + + if decoded_text.is_none() { + debug!("Failed to decode hash because HashDecoder::decode_hash_no_error_handling returned None"); + return None; + } + + println!("{}", decoded_text.as_ref().unwrap()); + + decoded_text + } +} + +fn decode_hash_no_error_handling(text: &str) -> Option { + // Runs the code to decode hash + // Doesn't perform error handling + + #[derive(Deserialize)] + #[serde(rename_all = "camelCase")] + struct Data { + body: std::collections::HashMap, + } + + #[derive(Deserialize)] + #[serde(rename_all = "PascalCase")] + struct Body { + plaintext: String, + } + + let mut data = HashMap::new(); + data.insert("Hash", [&text]); + + let client = reqwest::blocking::Client::new(); + let resp = client + .get("https://av5b81zg3k.execute-api.us-east-2.amazonaws.com/prod/lookup") + .json(&data) + .send() + .unwrap(); + + println!("{resp:?}"); + + match resp.status() { + reqwest::StatusCode::OK => { + let mut hash_out: Data = resp.json().ok()?; + + if let Some(result) = hash_out.body.remove(text) { + Some(result.plaintext) + } else { + None + } + } + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::HashDecoder; + use crate::decoders::interface::{Crack, Decoder}; + + #[test] + fn successful_decoding() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder + .crack("098f6bcd4621d373cade4e832627b4f6") + .unwrap(); + + assert_eq!(result, "test"); + } + + #[test] + fn hash_decode_handles_panics() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("hello my name is panicky mc panic face!"); + if result.is_some() { + panic!("Decode_base64 did not return an option with Some.") + } else { + // If we get here, the test passed + // Because the base64_decoder.crack function returned None + // as it should do for the input + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_empty_string() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack(""); + if result.is_some() { + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_emoji() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("😂"); + if result.is_some() { + assert_eq!(true, true); + } + } +} diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index 58758395..191b976d 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -6,5 +6,6 @@ //! you will also need to make it a public module in this file. pub mod base64_decoder; +pub mod hash_decoder; pub mod interface; pub mod reverse_decoder; diff --git a/src/lib.rs b/src/lib.rs index d278c366..5999928e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ //! Ares is an automatic decoding and cracking tool. mod checkers; -mod decoders; +pub mod decoders; mod filtration_system; mod searchers; mod storage;