pub fn clean(line: &str) -> &str {
let mut s = line;
if let Some(rest) = s.strip_prefix('\u{feff}') {
s = rest;
}
s = s.trim_matches(|c: char| c.is_whitespace() || c == ',');
if s.len() >= 2 {
let b = s.as_bytes();
let first = b[0];
let last = b[b.len() - 1];
if (first == b'"' && last == b'"') || (first == b'\'' && last == b'\'') {
s = &s[1..s.len() - 1];
}
}
s
}
pub fn strip_label(s: &str) -> Option<&str> {
let (k, v) = s.split_once([':', '='])?;
let k = k
.trim()
.trim_matches('"')
.trim_matches('\'')
.to_ascii_lowercase();
if !matches!(
k.as_str(),
"nt" | "ntlm"
| "lm"
| "hash"
| "password"
| "pass"
| "pwd"
| "md5"
| "sha1"
| "sha256"
| "sha512"
| "bcrypt"
| "crypt"
| "nthash"
) {
return None;
}
let v = v
.trim()
.trim_matches(',')
.trim_matches('"')
.trim_matches('\'');
if v.is_empty() {
return None;
}
Some(v)
}
pub fn extract_candidates(s: &str) -> Vec<String> {
let mut out = Vec::new();
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'$' {
let start = i;
i += 1;
while i < bytes.len()
&& !matches!(
bytes[i],
b' ' | b'\t' | b'"' | b'\'' | b',' | b'}' | b']' | b'\r' | b'\n'
)
{
i += 1;
}
if i - start >= 6 {
out.push(s[start..i].to_string());
}
} else {
i += 1;
}
}
let plausible = |n: usize| matches!(n, 16 | 32 | 40 | 48 | 56 | 64 | 96 | 128);
let mut run_start: Option<usize> = None;
for (idx, b) in bytes.iter().enumerate() {
let is_hex = b.is_ascii_hexdigit();
match (is_hex, run_start) {
(true, None) => run_start = Some(idx),
(false, Some(start)) => {
let n = idx - start;
if plausible(n) {
out.push(s[start..idx].to_string());
}
run_start = None;
}
_ => {}
}
}
if let Some(start) = run_start {
let n = bytes.len() - start;
if plausible(n) {
out.push(s[start..bytes.len()].to_string());
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clean_strips_bom_crlf_and_wrapping_quotes() {
assert_eq!(clean("\u{feff}hello\r\n"), "hello");
assert_eq!(clean(" \tindented \r"), "indented");
assert_eq!(clean("\"quoted\""), "quoted");
assert_eq!(clean("'single-quoted'"), "single-quoted");
assert_eq!(clean("trailing,"), "trailing");
assert_eq!(clean("\"unbalanced"), "\"unbalanced");
}
#[test]
fn strip_label_handles_common_shapes() {
assert_eq!(
strip_label("NT: 8846f7eaee8fb117ad06bdd830b7586c"),
Some("8846f7eaee8fb117ad06bdd830b7586c")
);
assert_eq!(
strip_label("password = $2a$05$abcdef"),
Some("$2a$05$abcdef")
);
assert_eq!(
strip_label("\"nthash\": \"8846f7eaee8fb117ad06bdd830b7586c\","),
Some("8846f7eaee8fb117ad06bdd830b7586c")
);
assert!(strip_label("filename: config.yaml").is_none());
assert!(strip_label("just-a-hash").is_none());
}
#[test]
fn extract_pulls_structured_hashes_out_of_wrappers() {
let line = r#"{"user": "bob", "creds": "$2a$05$LhayLxezLhK1LhWvKxCyLOj0j1u.Kj0jZ0pEmm134uzrQlFvQJLF6"}"#;
let cs = extract_candidates(line);
assert!(
cs.iter().any(|c| c.starts_with("$2a$05$")),
"structured hash not extracted"
);
}
#[test]
fn extract_pulls_hex_of_plausible_lengths() {
let line = "user,bob,hash,8846f7eaee8fb117ad06bdd830b7586c,extra";
let cs = extract_candidates(line);
assert!(cs.iter().any(|c| c == "8846f7eaee8fb117ad06bdd830b7586c"));
}
#[test]
fn extract_does_not_return_short_or_wrong_length_hex() {
let cs = extract_candidates("deadbeef abc123ef0987654 ffffffffffffffffffffffffffffffffff");
assert!(cs.is_empty(), "leaked: {cs:?}");
}
#[test]
fn extract_from_hashcat_show_output() {
let cs = extract_candidates("5f4dcc3b5aa765d61d8327deb882cf99:password123");
assert_eq!(cs.len(), 1);
assert_eq!(cs[0], "5f4dcc3b5aa765d61d8327deb882cf99");
}
}