From 3ee173748180140deb9f770de2714d7fbc66315b Mon Sep 17 00:00:00 2001 From: mehbark Date: Tue, 26 Nov 2024 18:55:56 -0500 Subject: [PATCH] whitespace hashmashing rust --- Cargo.lock | 198 +++++++++++++++++++++++++++++++++++++++++----------- Cargo.toml | 3 +- src/main.rs | 183 +++++++++++++++--------------------------------- 3 files changed, 218 insertions(+), 166 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88e46c1..33c2eb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,25 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "amulets" +version = "0.1.0" +dependencies = [ + "itertools", + "rand", + "regex", + "sha2", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -11,6 +30,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cfg-if" version = "1.0.0" @@ -26,31 +51,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crossbeam-deque" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" - [[package]] name = "crypto-common" version = "0.1.6" @@ -87,6 +87,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "itertools" version = "0.13.0" @@ -103,34 +114,97 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" [[package]] -name = "pearls" -version = "0.1.0" +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" dependencies = [ - "itertools", - "rayon", - "sha2", + "zerocopy", ] [[package]] -name = "rayon" -version = "1.10.0" +name = "proc-macro2" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ - "either", - "rayon-core", + "unicode-ident", ] [[package]] -name = "rayon-core" -version = "1.12.1" +name = "quote" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ - "crossbeam-deque", - "crossbeam-utils", + "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "sha2" version = "0.10.8" @@ -142,14 +216,58 @@ dependencies = [ "digest", ] +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index e31db7b..ac0f503 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2021" [dependencies] itertools = "0.13.0" -rayon = "1.10.0" +rand = "0.8.5" +regex = "1.11.1" sha2 = "0.10.8" diff --git a/src/main.rs b/src/main.rs index 8c51f4e..599f352 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,129 +1,62 @@ -use std::process::exit; +use std::{env, process}; -use itertools::Itertools; -use rayon::prelude::*; +use rand::{seq::SliceRandom, thread_rng}; +use regex::Regex; use sha2::{Digest, Sha256}; -// we could be smart with prefixes -#[allow(clippy::too_many_lines)] -fn main() { - let choices = [ - vec![ - "", - "they say ", - "they say that ", - "it's true! ", - "it's true: ", - "did you know ", - "did you know that ", - ], - vec!["the "], - vec![ - "sha", - "Sha", - "SHA", - "sha-", - "SHA-", - "Sha-", - "sha ", - "Sha ", - "SHA ", - "Secure Hash Algorithm ", - "secure hash algorithm ", - ], - vec![ - "256", - "2 5 6", - "two five six", - "two-fifty-six", - "two hundred and fifty six", - ], - vec![" "], - vec!["hash", "digest"], - vec![" of "], - vec!["this sentence", "this message", "this piece of digitext"], - vec![" has "], - vec![ - "ten", - "10", - "10 (ten)", - "ten (10)", - "10!", - "10(!)", - "ten!", - "ten(!)", - "10!!", - "10(!!)", - "ten!!", - "ten(!!)", - "10!!!", - "10(!!!)", - "ten!!!", - "ten(!!!)", - "10!!!!!!!!!!", - "10(!!!!!!!!!!)", - "ten!!!!!!!!!!", - "ten(!!!!!!!!!!)", - ], - vec![" "], - vec![ - "0s", - "zeroes", - "0s!", - "0s(!)", - "zeroes!", - "zeroes(!)", - "0s!!", - "0s(!!)", - "zeroes!!", - "zeroes(!!)", - "0s!!!", - "0s(!!!)", - "zeroes!!!", - "zeroes(!!!)", - "0s!!!!!!!!!!", - "0s(!!!!!!!!!!)", - "zeroes!!!!!!!!!!", - "zeroes(!!!!!!!!!!)", - ], - vec![" "], - vec![ - "in a row", - "in one group", - "all together", - "all together :)", - ], - vec!["! ", ". ", " "], - vec![ - "isn't that", - "i think that's", - "that's", - "that is", - "i think that is", - "that fact is", - ], - vec![" "], - vec!["somewhat ", "pretty ", "quite ", "very ", "decently ", ""], - vec!["fun", "nice", "sweet", "pleasant", "cool", "interesting"], - vec![" "], - vec![ - ":)", ":D", ":))", ":)))", "XD", ":B", ">:)", ">:D", ">XD", ">:B", "B)", "BD", ">B)", - ">BD", "(:", "((:", "(((:", "(:<", - ], - ]; - choices - .into_iter() - .multi_cartesian_product() - .par_bridge() - .for_each(|selection| { - let mut hasher = Sha256::new(); - for choice in &selection { - hasher.update(choice); - } - let digest = hasher.finalize(); - if format!("{digest:x}").contains("0000000000") { - println!("{}\n{digest:x}", selection.join("")); - exit(0); - } - }); +const WHITESPACE: &str = + " \t\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{200b}\u{202f}\u{205f}\u{3000}"; + +const DEFAULT_SEARCH: usize = 10; + +fn main() -> Result<(), &'static str> { + let mut args = env::args(); + args.next(); + + let Some(base) = args.next() else { + return Err("expected first arg to be the base message"); + }; + let base = base.trim(); + let base = { + let mut str = String::with_capacity(base.len() + DEFAULT_SEARCH); + str.push_str(base); + str + }; + + let Some(regex) = args.next().and_then(|s| s.trim().parse::().ok()) else { + return Err("expected second arg to be a valid regex for the digest"); + }; + + let mut whitespace: Vec = WHITESPACE.chars().collect(); + whitespace.shuffle(&mut thread_rng()); + + search(&base, ®ex, &whitespace, DEFAULT_SEARCH); + + eprintln!("i give up!"); + process::exit(2); +} + +fn search(base: &String, regex: &Regex, whitespace: &[char], depth: usize) { + if depth == 0 { + return; + } + + let digest = { + let mut hasher = Sha256::new(); + hasher.update(base); + format!("{:x}", hasher.finalize()) + }; + + if regex.is_match(&digest) { + println!("{base}"); + eprintln!("{base:?}"); + eprintln!("{digest}"); + process::exit(0); + } + + for ws in whitespace { + let mut next = base.clone(); + next.push(*ws); + search(&next, regex, whitespace, depth - 1); + } }