bhcli

"Strange's fork of n0tr1v's bhcli (onion)"
git clone https://git.dasho.dev/Strange/bhcli.git
Log | Files | Refs | README

commit 5eca6b026c1f8ab14f824d763df4874912febc78
parent af2fac2e6bd53fcb32de79e6107ebd4951de8869
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 30 Mar 2023 13:07:16 -0700

cleanup

Diffstat:
Msrc/lechatphp/mod.rs | 46+++++++++++++++++++++++-----------------------
1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/lechatphp/mod.rs b/src/lechatphp/mod.rs @@ -105,18 +105,18 @@ pub fn solve_b64(b64_str: &str) -> Option<String> { } fn _solve_difficulty1(img: &DynamicImage) -> Option<String> { - let nb_chars = 5; - let left_padding = 5; - let top_padding = 7; - let letter_width = 8; - let letter_height = 14; + const NB_CHARS: u32 = 5; + const LEFT_PADDING: u32 = 5; + const TOP_PADDING: u32 = 7; + const LETTER_WIDTH: u32 = 8; + const LETTER_HEIGHT: u32 = 14; let mut answer = String::new(); - for i in 0..nb_chars { - let sub_img = img.crop_imm(left_padding + ((letter_width+1)*i), top_padding, letter_width, letter_height); + for i in 0..NB_CHARS { + let sub_img = img.crop_imm(LEFT_PADDING + ((LETTER_WIDTH+1)*i), TOP_PADDING, LETTER_WIDTH, LETTER_HEIGHT); let mut buf = vec![]; { let mut enc = image::codecs::gif::GifEncoder::new(&mut buf); - enc.encode(sub_img.as_bytes(), letter_width, letter_height, ColorType::Rgba8).ok()?; + enc.encode(sub_img.as_bytes(), LETTER_WIDTH, LETTER_HEIGHT, ColorType::Rgba8).ok()?; } let letter_b64 = general_purpose::STANDARD.encode(&buf); let letter_b64 = letter_b64.strip_prefix(B64_PREFIX)?; @@ -133,14 +133,14 @@ fn _solve_difficulty1(img: &DynamicImage) -> Option<String> { // This function can solve both difficulty 1 and 2. fn solve_difficulty2(img: &DynamicImage) -> Option<String> { - let nb_chars = 5; - let left_padding = 5; - let top_padding = 7; - let letter_width = 8; - let letter_height = 14; + const NB_CHARS: u32 = 5; + const LEFT_PADDING: u32 = 5; + const TOP_PADDING: u32 = 7; + const LETTER_WIDTH: u32 = 8; + const LETTER_HEIGHT: u32 = 14; let mut answer = String::new(); - for i in 0..nb_chars { - let sub_img = img.crop_imm(left_padding + ((letter_width+1)*i), top_padding, letter_width, letter_height); + for i in 0..NB_CHARS { + let sub_img = img.crop_imm(LEFT_PADDING + ((LETTER_WIDTH +1)*i), TOP_PADDING, LETTER_WIDTH, LETTER_HEIGHT); for c in ALPHABET1.chars() { let good_letter_img = get_letter_img(c); if img_contains_letter(&sub_img, &good_letter_img) { @@ -249,17 +249,17 @@ fn solve_difficulty3(img: &DynamicImage) -> Result<String, CaptchaErr> { } fn find_letters(img: &DynamicImage) -> HashMap<char, Letter> { - let image_width = 150; - let image_height = 200; - let min_px_for_letter = 21; + const IMAGE_WIDTH: u32 = 150; + const IMAGE_HEIGHT: u32 = 200; + const MIN_PX_FOR_LETTER: usize = 21; let mut letters_map: HashMap<char, Letter> = HashMap::new(); - for y in 0..image_height { - for x in 0..image_width { + for y in 0..IMAGE_HEIGHT { + for x in 0..IMAGE_WIDTH { let letter_img = img.crop_imm(x, y, 8, 14); // We know that minimum amount of pixels on to form a letter is 21 // We can skip squares that do not have this prerequisite - if count_px_on(&letter_img) < min_px_for_letter { + if count_px_on(&letter_img) < MIN_PX_FOR_LETTER { continue } @@ -299,13 +299,13 @@ fn find_letters(img: &DynamicImage) -> HashMap<char, Letter> { } fn get_starting_letter(img: &DynamicImage, letters_map: &HashMap<char, Letter>) -> Option<Letter> { - let min_starting_pt_red_px = 50; + const MIN_STARTING_PT_RED_PX: usize = 50; let mut starting: Option<Letter> = None; // Step2: Find the starting letter for (_, letter) in letters_map.iter() { let square = img.crop_imm(letter.offset.x-5, letter.offset.y-3, 8+5+6, 14+3+2); let count_red = count_red_px(&square); - if count_red > min_starting_pt_red_px { + if count_red > MIN_STARTING_PT_RED_PX { starting = Some(letter.clone()); break; }