1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! KeyHog Scanner: A high-performance, multi-layered secret detection engine.
//!
//! This crate implements the core scanning logic, combining SIMD pre-filtering,
//! Aho-Corasick literal matching, regex fallback, and ML-based confidence scoring.
//!
//! # Module map (by pipeline stage)
//!
//! The modules below are declared in dependency order, but they READ in pipeline
//! order, the same bytes→finding flow as [`docs/src/architecture.md`] and the
//! method-level map in [`engine`] (`engine::mod` "# The one flow"). To find a
//! responsibility, locate its stage:
//!
//! - **Config / state / shared types**: [`scanner_config`], [`scan_state`],
//! [`types`], [`hw_probe`] (hardware routing), [`error`].
//! - **Phase 1 · prefilter** (cheap "could a detector fire here?")
//! [`alphabet_filter`], [`bigram_bloom`], [`prefix_trie`], `ascii_ci`,
//! `simd` / `simdsieve_prefilter` (feature-gated), `prefilter_degrade`
//! (loud Law-10 fallback).
//! - **Compile and lifecycle** (detectors → matchers): `compiled_scanner/`,
//! [`compiler`], `shared_regexes`, [`static_intern`].
//! - **Scan engine** (phase 1 triggers + phase 2 extraction; CPU or GPU):
//! [`engine`] (start at its header doc), [`pipeline`], [`gpu`]. Public scan
//! entry methods live in `compiled_scanner/runtime.rs` and dispatch here.
//! - **Decode-through** (nested base64/hex/url/unicode, recursive)
//! [`decode`], [`decode_structure`].
//! - **Entropy**: [`entropy`] is now the single home for all of it: the
//! keyword/scanner detection logic plus the fast Shannon-entropy primitive
//! `entropy::fast` (+ `entropy::avx512` / `entropy::fast_x86` /
//! `entropy::fast_neon` SIMD impls, arch-gated).
//! - **Confidence / ML**: [`ml_scorer`] (serves the embedded `weights.bin`;
//! trained out-of-band by the repo's `ml/`), [`confidence`],
//! `probabilistic_gate`.
//! - **Context, fragment reassembly, multiline, suppression, resolution**
//! [`context`], `fragment_cache`, [`multiline`], `suppression`,
//! [`resolution`], `structured`.
//! - **Specialized validators**: [`checksum`], [`jwt`], [`aws`],
//! `homoglyph`, [`unicode_hardening`].
//! - **Cross-cutting**: `platform_compat`, `placeholder_words`, [`telemetry`],
//! `util_hash`.
//!
//! Most single-file modules are one responsibility each; the multi-file engine
//! is the exception and carries its own internal map in `engine::mod`.
use Cow;
extern crate self as keyhog_scanner;
// ── Public API ──────────────────────────────────────────────────────
pub
/// Compiled detector-owned assignment-key admission index.
/// Tier-B generic credential-assignment keyword vocabulary (phase-2 prefilter).
pub
/// Offline AWS account-ID recovery from an access-key ID (no network/verify).
/// Service-specific credential checksum validation (GitHub, npm, Slack, etc.).
/// Compiled scanner construction and lifecycle implementation.
/// Detector compilation into high-performance matching structures.
pub
/// Heuristic and ML-based confidence scoring for candidate matches.
pub
/// Code context analysis (comments, assignments, test files).
pub
pub
pub
/// Decode-through pipeline for nested encodings (base64, hex, URL, etc.).
/// Decode-structure analysis: classify what a candidate base64/hex-decodes to
/// (binary asset magic bytes, protobuf wire) so decode-through feeds scoring.
pub
pub
/// Cache-local detector facts used by candidate execution and emission.
pub
/// Canonical detector-id strings and scanner-side detector-family predicates.
pub
/// Compiled canonical/decoded key-material policy from detector TOMLs.
pub
/// Compiled cache-local form of detector-owned model policy.
pub
/// Unified detector-indexed runtime plan compiled from detector TOMLs.
pub
/// Core scan execution engine.
pub
/// Shannon entropy analysis for secret detection.
/// Tier-B per-family generic-detector entropy-floor calibration table.
/// Specialized error types for the scanner.
pub
/// Cross-chunk fragment reassembly cache.
pub
/// Detector-owned generic assignment value-shape adjudication.
/// Named-detector ownership for assignment-key fallback suppression.
pub
/// GPU-accelerated matching via wgpu.
/// Scanner GPU batch input policy.
pub
/// GPU literal artifact compilation from the typed detector plan.
pub
/// Persistent GPU matcher artifact cache.
pub
/// Hardware capability detection and backend selection.
/// Machine learning inference for secret scoring.
/// Multiline secret reassembly logic.
pub
/// Pure phase-two regex truncation and UTF-8 focus boundaries.
pub
pub
pub
/// Match resolution and deduplication.
/// Process-wide scan profiling and diagnostics.
pub
/// Runtime match heap, interners, and ML pending queue for one scan.
pub
/// Scanner configuration and state.
pub
/// Tier-B distinctive vendor secret-prefix vocabulary for the multiline no-hit gate.
pub
/// Coalesced match-to-input attribution primitive.
pub
/// Static-string interner backed by a single-hash `ahash` map.
/// Used by `CompiledScanner` to pre-intern detector metadata strings
/// so the per-scan `ScanState` interner is hit only by dynamic
/// strings (file paths, commit SHAs).
pub
/// Shared types for the scanner engine.
pub
// Internal modules.
pub
/// SIMD-accelerated alphabet pre-filtering.
pub
pub
/// ASCII case-insensitive byte-search primitives shared by every hot path
/// that needs to skim text without lowering the haystack first.
pub
/// Bigram bloom filter for fast chunk gating.
pub
// The fast Shannon-entropy primitives (scalar dispatcher + AVX-512 / AVX2-SSE2 /
// NEON SIMD impls) now live UNDER `entropy/` (entropy::fast / ::avx512 /
// ::fast_x86 / ::fast_neon) (one home for all entropy code. See `entropy/mod.rs`).
pub
/// JWT structural validation and anomaly detection.
/// Internal scan pipeline orchestration.
pub
/// Prefix trie for efficient keyword propagation.
pub
pub
pub
pub
/// Per-scan telemetry: always-on counters + opt-in `--dogfood` events.
/// Shared parse + validate primitive for Tier-B single-column token lists
/// (assignment keywords, multiline secret prefixes) (one owner, no drift).
pub
pub
/// Unicode normalization and homoglyph defense.
pub
/// Shared FNV-1a hash + content-keyed memoization primitives. Single home for
/// the seed every per-scan cache keys on, plus the bounded thread-local cache
/// helper they all share, so a hash change can never re-key only some caches.
/// Loud, recall-preserving degradation for static prefilter automata (Law 10).
pub
pub use floor_char_boundary;
/// SHA-256 of a credential as the `CredentialHash` domain type. Re-exported
/// from the single canonical implementation in `keyhog_core` so the scanner,
/// core dedup, and telemetry all hash credentials identically (no second copy
/// to drift). Hex encoding is a separate step at the serde/reporter boundary
/// (`keyhog_core::hex_encode`), keeping the pre-dedup hot path zero-heap.
pub use sha256_hash;
pub use compute_line_offsets;
pub
pub
pub use *;
/// Configure the Hyperscan compiled-database cache directory for this process.
///
/// Call before compiling a scanner. `None` restores the platform default
/// (`dirs::cache_dir()/keyhog`, with a per-user temp fallback). The SIMD backend
/// still validates the final directory: explicit paths must live under the
/// user's home or the per-uid keyhog temp cache root, must be user-owned, and
/// must not be symlinks.
/// Validate an explicit Hyperscan cache directory without compiling a scanner.
/// True when `detector_id` names the pure-entropy fallback family (`"entropy"`
/// or any `"entropy-*"` id such as `entropy-token`).
///
/// Pure-entropy detectors fire on the Shannon entropy of the matched character
/// run rather than on a distinctive prefix/shape, so whether one fires for a
/// given secret is *context-dependent*: the same bytes embedded in a longer
/// token run (a connection-string URL, a `key=` assignment) can dilute below the
/// entropy gate even though they fire in isolation. Consumers that categorize a
/// finding by detector family, and the contract test harness, which must not
/// gate context-dependent firings all-or-nothing, use this to distinguish the
/// entropy fallback from service-anchored detectors without re-encoding the
/// naming contract owned by [`detector_ids`].
/// True for a detector that fires via the entropy / phase2-generic path (the
/// `generic-*` family + entropy fallback), carrying ZERO patterns by design.
/// Strip invisible-reorder evasion characters (zero-width + RTL override, per
/// [`unicode_hardening::is_evasion_char`]) from context-window text. Deliberately
/// narrower than [`unicode_hardening::normalize_homoglyphs`]: this feeds the
/// surrounding-context features, where collapsing homoglyphs/fullwidth/combining
/// marks in ordinary prose would distort keyword and comment context; homoglyph
/// folding stays on the credential-value scan path.
pub