whitespace hashmashing rust

This commit is contained in:
mehbark 2024-11-26 18:55:56 -05:00
parent 5a8a402d6b
commit 3ee1737481
3 changed files with 218 additions and 166 deletions

198
Cargo.lock generated
View file

@ -2,6 +2,25 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@ -11,6 +30,12 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -26,31 +51,6 @@ dependencies = [
"libc", "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]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.6" version = "0.1.6"
@ -87,6 +87,17 @@ dependencies = [
"version_check", "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]] [[package]]
name = "itertools" name = "itertools"
version = "0.13.0" version = "0.13.0"
@ -103,34 +114,97 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
[[package]] [[package]]
name = "pearls" name = "memchr"
version = "0.1.0" 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 = [ dependencies = [
"itertools", "zerocopy",
"rayon",
"sha2",
] ]
[[package]] [[package]]
name = "rayon" name = "proc-macro2"
version = "1.10.0" version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [ dependencies = [
"either", "unicode-ident",
"rayon-core",
] ]
[[package]] [[package]]
name = "rayon-core" name = "quote"
version = "1.12.1" version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [ dependencies = [
"crossbeam-deque", "proc-macro2",
"crossbeam-utils",
] ]
[[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]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.8" version = "0.10.8"
@ -142,14 +216,58 @@ dependencies = [
"digest", "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]] [[package]]
name = "typenum" name = "typenum"
version = "1.17.0" version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.5" version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 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",
]

View file

@ -5,5 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
itertools = "0.13.0" itertools = "0.13.0"
rayon = "1.10.0" rand = "0.8.5"
regex = "1.11.1"
sha2 = "0.10.8" sha2 = "0.10.8"

View file

@ -1,129 +1,62 @@
use std::process::exit; use std::{env, process};
use itertools::Itertools; use rand::{seq::SliceRandom, thread_rng};
use rayon::prelude::*; use regex::Regex;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
// we could be smart with prefixes const WHITESPACE: &str =
#[allow(clippy::too_many_lines)] " \t\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{200b}\u{202f}\u{205f}\u{3000}";
fn main() {
let choices = [ const DEFAULT_SEARCH: usize = 10;
vec![
"", fn main() -> Result<(), &'static str> {
"they say ", let mut args = env::args();
"they say that ", args.next();
"it's true! ",
"it's true: ", let Some(base) = args.next() else {
"did you know ", return Err("expected first arg to be the base message");
"did you know that ", };
], let base = base.trim();
vec!["the "], let base = {
vec![ let mut str = String::with_capacity(base.len() + DEFAULT_SEARCH);
"sha", str.push_str(base);
"Sha", str
"SHA", };
"sha-",
"SHA-", let Some(regex) = args.next().and_then(|s| s.trim().parse::<Regex>().ok()) else {
"Sha-", return Err("expected second arg to be a valid regex for the digest");
"sha ", };
"Sha ",
"SHA ", let mut whitespace: Vec<char> = WHITESPACE.chars().collect();
"Secure Hash Algorithm ", whitespace.shuffle(&mut thread_rng());
"secure hash algorithm ",
], search(&base, &regex, &whitespace, DEFAULT_SEARCH);
vec![
"256", eprintln!("i give up!");
"2 5 6", process::exit(2);
"two five six", }
"two-fifty-six",
"two hundred and fifty six", fn search(base: &String, regex: &Regex, whitespace: &[char], depth: usize) {
], if depth == 0 {
vec![" "], return;
vec!["hash", "digest"], }
vec![" of "],
vec!["this sentence", "this message", "this piece of digitext"], let digest = {
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(); let mut hasher = Sha256::new();
for choice in &selection { hasher.update(base);
hasher.update(choice); format!("{:x}", hasher.finalize())
};
if regex.is_match(&digest) {
println!("{base}");
eprintln!("{base:?}");
eprintln!("{digest}");
process::exit(0);
} }
let digest = hasher.finalize();
if format!("{digest:x}").contains("0000000000") { for ws in whitespace {
println!("{}\n{digest:x}", selection.join("")); let mut next = base.clone();
exit(0); next.push(*ws);
search(&next, regex, whitespace, depth - 1);
} }
});
} }