hashglass 0.1.0

Fast pentester-focused hash identifier — mode + cost + ready hashcat command. Handles secretsdump / containers / mixed dumps. Identification only.
Documentation
//! Slice 6 — cost annotation.
//!
//! Turns "which mode" into "should I even bother cracking this." Every hashcat
//! mode we cover gets a coarse speed class + a rough time-to-exhaust rockyou
//! (~14M candidates) on a **4090-class GPU**. Numbers are order-of-magnitude
//! reference points from published hashcat benchmarks, deliberately rounded —
//! precise per-rig numbers belong in `hashcat -b`, not here.
//!
//! Fast → seconds. VerySlow → weeks. That resolution is enough for the
//! decision this feature exists to inform ("skip, or commit the box?").

use std::time::Duration;

/// Coarse hashrate class. Ordered fastest→slowest so `derive(Ord)` gives a
/// natural sort key when we ever need to rank by cost.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Speed {
    /// > 1 GH/s — raw MD/SHA family. Anything cracks in seconds.
    Fast,
    /// ~100 MH/s — NTLM, single-round salted MD5.
    Medium,
    /// ~1–10 MH/s — sha256crypt/sha512crypt, DCC2, Office 2013, WPA-PMKID.
    Slow,
    /// < 100 kH/s — bcrypt, scrypt, KeePass, PBKDF2 with high iteration counts.
    VerySlow,
}

impl Speed {
    /// Approximate throughput on a 4090-class GPU, in hashes/second.
    /// Order-of-magnitude reference points from published hashcat benchmarks,
    /// tuned so rockyou-time comes out in the pentester-recognizable range
    /// (bcrypt ≈ hours-days, sha512crypt ≈ hours, MD5 ≈ instant).
    pub fn hashes_per_sec(self) -> f64 {
        match self {
            // Raw MD5 on a 4090 ≈ 65 GH/s. Even the "worst" fast mode is > 10 GH/s.
            Speed::Fast => 30e9,
            // NTLM ≈ 165 GH/s but this bucket also holds NetNTLMv2 (~100 MH/s)
            // and salted MD5 (~50 MH/s). Pick the lower end so we don't lie
            // about mid-hash cost.
            Speed::Medium => 50e6,
            // sha256crypt ≈ 300 kH/s; sha512crypt ≈ 200 kH/s; DCC2 ≈ 1 MH/s.
            // Rockyou at 400 kH/s ≈ 36s per pass — realistic for pentester GPUs.
            Speed::Slow => 400e3,
            // bcrypt cost=5 ≈ 200 kH/s, cost=12 ≈ 1 kH/s, KeePass ≈ 100 H/s.
            // The realistic pentester case is cost≥10 or KDF-heavy — go with
            // 1.5k so rockyou lands in days-weeks (the correct triage signal).
            Speed::VerySlow => 1.5e3,
        }
    }

    /// Human label, deliberately blunt.
    pub fn label(self) -> &'static str {
        match self {
            Speed::Fast => "fast",
            Speed::Medium => "medium",
            Speed::VerySlow => "very slow",
            Speed::Slow => "slow",
        }
    }
}

/// Explicit table over the modes hashglass covers. If a mode ISN'T here, we
/// don't guess a class from length — return `None` and let the UI stay silent
/// rather than make something up.
pub fn speed_for(mode: u32) -> Option<Speed> {
    Some(match mode {
        // Raw MD/SHA — fast.
        0 | 100 | 900 | 1000 | 1400 | 1700 | 10800 | 17400 | 17800 | 6000 | 6900 => Speed::Fast,
        // Salted single-round MD5/SHA-1/SHA-256 + Django + LDAP {SHA}/{SSHA} +
        // DCC(mscash), which shares the salted-MD5 wire shape.
        10 | 20 | 110 | 120 | 1410 | 1420 | 101 | 111 | 10000 | 400 | 7900 | 1100 => Speed::Medium,
        // NetNTLMv1/v2, JWT (HS/RS/ES), Kerberos 23-etype (RC4-HMAC).
        5500 | 5600 | 16500 | 13100 | 18200 | 7500 => Speed::Medium,
        // Kerberos AES etypes, Cisco $8$, sha*crypt, DCC2, apr1, md5crypt, sha1crypt.
        19600 | 19700 | 9200 | 1800 | 7400 | 2100 | 1600 | 500 | 15100 => Speed::Slow,
        // WPA-PBKDF2+EAPOL, Office 2013 (100k SHA-512 iters), PDF R6.
        22000 | 9600 | 10500 | 10600 | 10700 | 10400 => Speed::Slow,
        // bcrypt, scrypt, KeePass (AES/Twofish + very high KDF), DPAPI, archives.
        3200 | 9300 | 13400 | 15300 | 15900 | 11600 | 12500 | 13000 | 13600 | 9400 | 9500 => {
            Speed::VerySlow
        }
        // LM (56-bit DES per half) is trivially exhaustible — call it Fast.
        3000 => Speed::Fast,
        _ => return None,
    })
}

/// Estimate time to exhaust rockyou (~14.34M candidates) at this mode's class,
/// scaled by the detected GPU tier's multiplier (slice 13). On boxes with no
/// CUDA / no nvidia-smi the multiplier is 1.0 (baseline 4090), which reads as
/// "assume 4090" — better than silent-wrong on rented rigs the user might be
/// SSH-ing into.
pub fn rockyou_time(mode: u32) -> Option<Duration> {
    let rate = speed_for(mode)?.hashes_per_sec() * crate::gpu::tier().multiplier();
    let secs = 14_344_391.0 / rate;
    Some(Duration::from_secs_f64(secs))
}

/// A short one-liner for the auto-mode output. Empty string if we don't have
/// data — the caller can just skip printing. The GPU-label suffix comes from
/// [`crate::gpu::tier`], so "on a 4090-class GPU" appears when we didn't
/// detect one, and the detected tier's label otherwise.
pub fn oneliner(mode: u32) -> String {
    let Some(speed) = speed_for(mode) else {
        return String::new();
    };
    let rockyou = rockyou_time(mode)
        .map(humanize)
        .unwrap_or_else(|| "?".into());
    format!(
        "{} — rockyou ≈ {rockyou} {}",
        speed.label(),
        crate::gpu::tier().label()
    )
}

/// A blunt human duration string tuned for the specific job here:
/// "3s", "45min", "6h", "3d", "3wk". No hair-splitting.
pub fn humanize(d: Duration) -> String {
    let s = d.as_secs_f64();
    if s < 1.0 {
        return "instant".into();
    }
    if s < 60.0 {
        return format!("{}s", s.round() as u64);
    }
    if s < 3600.0 {
        return format!("{}min", (s / 60.0).round() as u64);
    }
    if s < 86_400.0 {
        return format!("{}h", (s / 3600.0).round() as u64);
    }
    if s < 86_400.0 * 14.0 {
        return format!("{}d", (s / 86_400.0).round() as u64);
    }
    if s < 86_400.0 * 365.0 {
        return format!("{}wk", (s / (86_400.0 * 7.0)).round() as u64);
    }
    "years".into()
}

/// Concrete crack-strategy advice per speed class. This is where "identifier"
/// becomes "advisor" — the thing rivals don't do.
pub fn advice(mode: u32) -> Option<&'static str> {
    Some(match speed_for(mode)? {
        Speed::Fast => {
            "full wordlist + rules is trivial — go straight for rockyou --rules=OneRuleToRuleThemAll"
        }
        Speed::Medium => "full rockyou is fine; add --rules for +10-30% recovery",
        Speed::Slow => "top-1M wordlist first, then targeted masks; full rockyou may be hours",
        Speed::VerySlow => {
            "high-value only — top-10k words + best64 rule. Full rockyou = days-weeks"
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_covered_mode_has_a_speed_class() {
        // The modes hashglass identifies via structured/prefixed rules —
        // these are the ones that surface in output and therefore MUST have
        // cost data. Bare-hex ambiguous tier is looser and covered above.
        for mode in [
            500, 1600, 7400, 1800, 3200, 15100, 400, 7900, 10000, 101, 111, 9200, 9300, 13100,
            19600, 19700, 18200, 7500, 9400, 9500, 9600, 13400, 15300, 15900, 11600, 12500, 13000,
            13600, 10400, 16500, 5500, 5600, 2100, 22000, 10, 20, 110, 120, 1410, 1420, 1100, 0,
            900, 1000, 100, 1400, 1700, 3000,
        ] {
            assert!(speed_for(mode).is_some(), "no cost class for -m {mode}");
        }
    }

    #[test]
    fn bcrypt_is_hours_and_dwarfs_md5() {
        // The single most important pair: this is why the feature exists.
        // Assertion order-of-magnitude only — we calibrate to real hashcat
        // benchmarks but never promise per-rig precision. The relative gap
        // is invariant across GPU tiers (multipliers apply uniformly), so
        // the "bcrypt dwarfs MD5" claim holds regardless of detected tier.
        let bcrypt = rockyou_time(3200).unwrap();
        let md5 = rockyou_time(0).unwrap();
        assert!(
            md5 < bcrypt / 100_000,
            "MD5 should be ≥5 orders faster than bcrypt"
        );
        // Absolute floor: even on a datacenter GPU (2× baseline), bcrypt on
        // rockyou is still north of 30min. On anything slower, it's hours.
        assert!(
            bcrypt > Duration::from_secs(1200),
            "bcrypt on rockyou should be > 20min on any GPU tier"
        );
    }

    #[test]
    fn humanize_reads_naturally() {
        assert_eq!(humanize(Duration::from_secs_f64(0.3)), "instant");
        assert_eq!(humanize(Duration::from_secs(45)), "45s");
        assert_eq!(humanize(Duration::from_secs(60 * 12)), "12min");
        assert_eq!(humanize(Duration::from_secs(60 * 60 * 5)), "5h");
        assert_eq!(humanize(Duration::from_secs(86_400 * 3)), "3d");
        assert_eq!(humanize(Duration::from_secs(86_400 * 30)), "4wk");
    }

    #[test]
    fn advice_matches_speed_class() {
        assert!(advice(0).unwrap().contains("trivial")); // MD5
        assert!(advice(3200).unwrap().contains("high-value")); // bcrypt
        assert!(advice(9999).is_none()); // unknown mode → no advice
    }

    #[test]
    fn oneliner_for_bcrypt_mentions_slowness() {
        let s = oneliner(3200);
        assert!(s.contains("very slow"));
        assert!(s.contains("rockyou"));
    }
}