purple-ssh 3.15.16

Open-source terminal SSH manager that keeps ~/.ssh/config in sync with your cloud infra. Spin up a VM on AWS, GCP, Azure, Hetzner or 12 other cloud providers and it appears in your host list. Destroy it and the entry dims. Search hundreds of hosts, transfer files, manage Docker and Podman over SSH, sign Vault SSH certs. Rust TUI, MIT licensed.
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::UNIX_EPOCH;

use log::debug;

use crate::ssh_config::model::HostEntry;

thread_local! {
    /// Test-scoped override of the SSH directory. When set, `resolve_ssh_dir`
    /// returns this path instead of `~/.ssh`. Lets tests that exercise
    /// `discover_keys` or `finalize_key_push` point at a synthetic tree
    /// without touching the test runner's actual `~/.ssh`.
    static SSH_DIR_OVERRIDE: std::cell::RefCell<Option<PathBuf>> =
        const { std::cell::RefCell::new(None) };
}

/// Resolve the SSH directory to scan for keys. Honours a thread-local
/// override set by tests; falls back to `~/.ssh` in production. Returns
/// `None` when the home directory cannot be determined and no override
/// is set, so callers can short-circuit cleanly.
pub fn resolve_ssh_dir() -> Option<PathBuf> {
    let override_path = SSH_DIR_OVERRIDE.with(|p| p.borrow().clone());
    override_path.or_else(|| dirs::home_dir().map(|h| h.join(".ssh")))
}

/// Set the test-scoped SSH directory override. Scoped to the calling
/// thread. Restored by `clear_ssh_dir_override`.
#[cfg(test)]
pub fn set_ssh_dir_override(path: PathBuf) {
    SSH_DIR_OVERRIDE.with(|p| *p.borrow_mut() = Some(path));
}

/// Clear the test-scoped SSH directory override.
#[cfg(test)]
#[allow(dead_code)]
pub fn clear_ssh_dir_override() {
    SSH_DIR_OVERRIDE.with(|p| *p.borrow_mut() = None);
}

/// Information about an SSH key found on disk.
#[derive(Debug, Clone)]
pub struct SshKeyInfo {
    /// Display name (filename without path, e.g. "id_ed25519")
    pub name: String,
    /// Display path with tilde (e.g. "~/.ssh/id_ed25519")
    pub display_path: String,
    /// Key type (e.g. "ED25519", "RSA", "sk-ED25519")
    pub key_type: String,
    /// Key bits (e.g. "256", "4096")
    pub bits: String,
    /// SHA256 fingerprint
    pub fingerprint: String,
    /// Comment from the public key
    pub comment: String,
    /// Host aliases that reference this key via IdentityFile
    pub linked_hosts: Vec<String>,
    /// Drunken Bishop visual fingerprint from `ssh-keygen -lv`. 11 lines
    /// (top border + 9 content + bottom border), joined with `\n`. Empty
    /// when ssh-keygen returned no art block.
    pub bishop_art: String,
    /// Strength score 0..=100. Composed of algorithm strength, key size and
    /// on-disk encryption. Hardware-bound `sk-*` keys score highest;
    /// deprecated DSA and short RSA score lowest.
    pub strength_score: u8,
    /// Private key on disk is passphrase-encrypted. Detected via
    /// `ssh-keygen -y -P "" -f <key>` exit status. False when the private
    /// key is missing or unreadable.
    pub encrypted: bool,
    /// Public key fingerprint matches an entry returned by `ssh-add -l`.
    pub agent_loaded: bool,
    /// File is an OpenSSH user certificate. Detected via `-cert.pub`
    /// filename suffix or a `-cert` substring in the ssh-keygen-reported
    /// key type (see `detect_certificate`).
    pub is_certificate: bool,
    /// File mtime of the private key (or pubkey when private is missing),
    /// expressed as seconds since UNIX epoch. None when the file system
    /// cannot report a timestamp. Powers the `Modified` field on the
    /// Keys tab hero panel; mtime is the most portable proxy because
    /// birthtime is not exposed by every supported filesystem.
    pub mtime_ts: Option<u64>,
}

impl SshKeyInfo {
    /// Format type with bits (e.g. "ED25519" or "RSA 4096").
    pub fn type_display(&self) -> String {
        if self.bits.is_empty() {
            self.key_type.clone()
        } else {
            format!("{} {}", self.key_type, self.bits)
        }
    }

    /// Drunken Bishop art split into rendering-ready lines. Empty Vec when
    /// `bishop_art` is empty (e.g. when ssh-keygen failed at discovery).
    pub fn bishop_lines(&self) -> Vec<&str> {
        if self.bishop_art.is_empty() {
            Vec::new()
        } else {
            self.bishop_art.lines().collect()
        }
    }
}

/// Character ladder for the Drunken Bishop random-art. Index 0 is the
/// unvisited cell; counter values 1..=13 map to ascending visit density;
/// 15 marks the bishop's start position (S) and 16 the end position (E).
const BISHOP_CHARS: &[u8] = b" .o+=*BOX@%&#/^SE";

const BISHOP_COUNTER_CAP: u8 = 14;
const BISHOP_S_INDEX: u8 = 15;
const BISHOP_E_INDEX: u8 = 16;

/// Decode an OpenSSH `SHA256:<base64>` fingerprint string into its raw
/// hash bytes. OpenSSH emits unpadded base64 with potentially non-zero
/// trailing bits (synthetic fingerprints from demo fixtures sometimes
/// fall in this bucket), so we configure a lenient engine that accepts
/// both padded and unpadded input. Returns `None` when the `SHA256:`
/// prefix is missing or the body is not decodable.
pub fn decode_fingerprint(fp_str: &str) -> Option<Vec<u8>> {
    use base64::Engine;
    use base64::engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig};
    let b64 = fp_str.strip_prefix("SHA256:")?;
    let config = GeneralPurposeConfig::new()
        .with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent)
        .with_decode_allow_trailing_bits(true);
    let engine = GeneralPurpose::new(&base64::alphabet::STANDARD, config);
    engine.decode(b64).ok()
}

/// Generate the Drunken Bishop visit grid from a fingerprint. Mirrors the
/// `key_fingerprint_randomart` walk in OpenSSH `sshkey.c`: starting at
/// the grid centre, the bishop steps diagonally based on 2-bit pairs read
/// LSB-first from each fingerprint byte. Each visited cell increments its
/// counter (capped so unique cells stay distinguishable), and the start
/// and end positions are tagged with `S` and `E` markers.
///
/// `cols` and `rows` are the interior cell dimensions. Use 17×9 to match
/// the canonical OpenSSH output, or scale up for a more prominent visual.
pub fn drunken_bishop_grid(fp_bytes: &[u8], cols: usize, rows: usize) -> Vec<Vec<u8>> {
    let mut grid = vec![vec![0u8; cols]; rows];
    let mut x = cols / 2;
    let mut y = rows / 2;
    let start = (x, y);
    for &byte in fp_bytes {
        let mut b = byte;
        for _ in 0..4 {
            let dx: isize = if b & 0x1 == 0 { -1 } else { 1 };
            let dy: isize = if b & 0x2 == 0 { -1 } else { 1 };
            x = (x as isize + dx).clamp(0, cols as isize - 1) as usize;
            y = (y as isize + dy).clamp(0, rows as isize - 1) as usize;
            if grid[y][x] < BISHOP_COUNTER_CAP - 1 {
                grid[y][x] += 1;
            }
            b >>= 2;
        }
    }
    grid[start.1][start.0] = BISHOP_S_INDEX;
    grid[y][x] = BISHOP_E_INDEX;
    grid
}

/// Map a Drunken Bishop counter value to its display character.
pub fn bishop_char(counter: u8) -> char {
    let idx = counter.min(BISHOP_E_INDEX) as usize;
    BISHOP_CHARS[idx] as char
}

/// Translate a UI-space selection index into an `app.keys.list` index,
/// honoring an active search filter. Returns `None` when the selection
/// is out of range for the current filter. Centralised here so every
/// call site (copy, push, detail-pane render) maps selection back to
/// `app.keys.list` through one code path; a divergent implementation in any
/// of them would silently point at the wrong key.
pub fn resolve_selection(keys: &[SshKeyInfo], query: Option<&str>, sel: usize) -> Option<usize> {
    let filtered = filtered_key_indices(keys, query);
    filtered.get(sel).copied()
}

/// Indices into `keys` whose `name` or `comment` contains `query`
/// (case-insensitive substring). Returns all indices when the query is
/// empty or `None`, so callers can render the unfiltered list using the
/// same code path. Pure function so the search handler can call it
/// repeatedly per keystroke without touching the App.
pub fn filtered_key_indices(keys: &[SshKeyInfo], query: Option<&str>) -> Vec<usize> {
    match query {
        None | Some("") => (0..keys.len()).collect(),
        Some(q) => {
            let needle = q.to_ascii_lowercase();
            keys.iter()
                .enumerate()
                .filter(|(_, k)| {
                    k.name.to_ascii_lowercase().contains(&needle)
                        || k.comment.to_ascii_lowercase().contains(&needle)
                })
                .map(|(i, _)| i)
                .collect()
        }
    }
}

/// Discover SSH keys in the given directory and cross-reference with host entries.
///
/// Runs `ssh-add -l` once at the start so each key knows whether its
/// fingerprint is currently loaded in the agent. The result is a snapshot:
/// keys added to or removed from the agent after this call will not show
/// up until the next discover_keys() invocation (host reload).
pub fn discover_keys(ssh_dir: &Path, hosts: &[HostEntry]) -> Vec<SshKeyInfo> {
    let entries = match std::fs::read_dir(ssh_dir) {
        Ok(entries) => entries,
        Err(_) => return Vec::new(),
    };

    let home = dirs::home_dir();
    let agent_fingerprints = agent_loaded_fingerprints();

    let mut keys: Vec<SshKeyInfo> = entries
        .filter_map(|e| e.ok())
        .filter(is_public_key_file)
        .filter_map(|e| {
            read_key_info(
                ssh_dir,
                &e.path(),
                home.as_deref(),
                hosts,
                &agent_fingerprints,
            )
        })
        .collect();

    keys.sort_by(|a, b| a.name.cmp(&b.name));
    debug!(
        "[purple] discover_keys: found {} key(s) in {}, {} loaded in agent",
        keys.len(),
        ssh_dir.display(),
        agent_fingerprints.len()
    );
    keys
}

/// Fingerprints (SHA256 form, including the `SHA256:` prefix) of every key
/// currently loaded in the running ssh-agent. Empty when the agent has no
/// identities, is not reachable, or `ssh-add` is missing. Each failure
/// path emits one debug line so a user reporting "agent column always
/// reads `not loaded`" has a trace pointing at the cause.
fn agent_loaded_fingerprints() -> HashSet<String> {
    let output = Command::new("ssh-add").arg("-l").output();
    match output {
        Ok(o) if o.status.success() => parse_agent_list(&String::from_utf8_lossy(&o.stdout)),
        Ok(o) => {
            let code = o.status.code().unwrap_or(-1);
            let stderr = String::from_utf8_lossy(&o.stderr);
            log::debug!(
                "[external] ssh-add -l non-zero exit={code} stderr={}",
                stderr.trim().lines().next().unwrap_or("<empty>"),
            );
            HashSet::new()
        }
        Err(e) => {
            log::debug!("[external] ssh-add spawn failed: {e}");
            HashSet::new()
        }
    }
}

/// Parse `ssh-add -l` stdout into a fingerprint set. Each line has the
/// format `<bits> SHA256:<hash> <comment> (<TYPE>)`; we extract column 2.
/// Lines that do not start with a numeric bit count are skipped (covers
/// the "The agent has no identities." string and any future banner).
fn parse_agent_list(stdout: &str) -> HashSet<String> {
    stdout
        .lines()
        .filter_map(|line| {
            let parts: Vec<&str> = line.splitn(3, ' ').collect();
            if parts.len() >= 2 && parts[1].starts_with("SHA256:") {
                Some(parts[1].to_string())
            } else {
                None
            }
        })
        .collect()
}

/// Compute the strength score for a key. Pure function so we can unit-test
/// every algorithm/bit combo without subprocess calls. Hardware-bound `sk-*`
/// keys are floored at 90 since the private material never leaves the token;
/// deprecated DSA and short RSA collapse to single-digit scores.
fn strength_score_for(key_type: &str, bits: &str, encrypted: bool) -> u8 {
    // OpenSSH spells hardware-key types as `sk-ed25519` / `sk-ecdsa-...`,
    // but ssh-keygen output sometimes uppercases the prefix. One
    // case-insensitive prefix check covers both.
    let is_sk = key_type.to_ascii_lowercase().starts_with("sk-");
    let base: i16 = if is_sk {
        95
    } else {
        match key_type.to_ascii_uppercase().as_str() {
            "DSA" => 5,
            "RSA" => match bits.parse::<u32>().unwrap_or(0) {
                0..=1023 => 5,
                1024..=2047 => 15,
                2048..=3071 => 55,
                3072..=4095 => 75,
                _ => 80,
            },
            "ECDSA" => match bits.parse::<u32>().unwrap_or(0) {
                256 => 70,
                384 => 80,
                521 => 85,
                _ => 60,
            },
            "ED25519" => 90,
            _ => 50,
        }
    };
    let modifier: i16 = if encrypted { 5 } else { -10 };
    (base + modifier).clamp(0, 100) as u8
}

/// Detect whether a private key file is passphrase-encrypted by trying
/// to derive its public key with an empty passphrase. Empty-passphrase
/// success means unencrypted; failure means encrypted (or unreadable).
/// Returns false when the private key file is absent so unbacked .pub
/// files do not get flagged as encrypted.
fn private_key_encrypted(private_path: &Path) -> bool {
    if !private_path.exists() {
        return false;
    }
    let output = Command::new("ssh-keygen")
        .arg("-y")
        .args(["-P", ""])
        .arg("-f")
        .arg(private_path)
        .output();
    match output {
        Ok(o) => !o.status.success(),
        Err(_) => false,
    }
}

/// Extract the Drunken Bishop ASCII block from `ssh-keygen -lv` stdout.
/// Returns the 11 art lines joined with `\n`, or an empty string when the
/// expected `+--...--+` border + 9 content rows + closing border are not
/// all present. The filter matches any line that opens AND closes with
/// either `+` (border) or `|` (content), which is robust to header
/// variations across OpenSSH versions.
fn parse_bishop_block(stdout: &str) -> String {
    let art_lines: Vec<&str> = stdout
        .lines()
        .filter(|l| {
            let t = l.trim_end();
            (t.starts_with('+') && t.ends_with('+')) || (t.starts_with('|') && t.ends_with('|'))
        })
        .collect();
    if art_lines.len() == 11 {
        art_lines.join("\n")
    } else {
        String::new()
    }
}

/// Check if a directory entry looks like a public key file.
fn is_public_key_file(entry: &std::fs::DirEntry) -> bool {
    let name = entry.file_name();
    let name = name.to_string_lossy();

    // Must end in .pub
    if !name.ends_with(".pub") {
        return false;
    }

    // Skip known non-key files
    let skip = ["authorized_keys.pub", "known_hosts.pub"];
    if skip.contains(&name.as_ref()) {
        return false;
    }

    // Use std::fs::metadata, not DirEntry::file_type or DirEntry::metadata:
    // both of those use lstat and report the symlink itself (is_file = false).
    // std::fs::metadata uses stat, follows the chain, and reports the target.
    std::fs::metadata(entry.path())
        .map(|m| m.is_file())
        .unwrap_or(false)
}

/// Read key metadata using `ssh-keygen -lv` (fingerprint + Drunken Bishop)
/// and cross-reference with hosts, agent state and on-disk encryption.
fn read_key_info(
    ssh_dir: &Path,
    pub_path: &Path,
    home: Option<&Path>,
    hosts: &[HostEntry],
    agent_fingerprints: &HashSet<String>,
) -> Option<SshKeyInfo> {
    let output = Command::new("ssh-keygen")
        .arg("-lv")
        .arg("-f")
        .arg(pub_path)
        .args(["-E", "sha256"])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let first_line = stdout.lines().next()?.trim();

    // Format: "<bits> <fingerprint> <comment> (<type>)"
    let (bits, fingerprint, comment, key_type) = parse_keygen_output(first_line)?;

    // Derive the private key name (strip .pub)
    let pub_name = pub_path.file_name()?.to_string_lossy();
    let name = pub_name
        .strip_suffix(".pub")
        .unwrap_or(&pub_name)
        .to_string();

    // Private key path (without .pub extension)
    let private_path = ssh_dir.join(&name);

    // Display path: use ~ if ssh_dir is under home
    let display_path = match home {
        Some(home) if ssh_dir.starts_with(home) => {
            let relative = ssh_dir.strip_prefix(home).unwrap();
            format!("~/{}/{}", relative.display(), name)
        }
        _ => private_path.display().to_string(),
    };

    // Find hosts that reference this key
    let linked_hosts = find_linked_hosts(&private_path, &display_path, hosts);

    // Extract Drunken Bishop ASCII block from -lv output.
    let bishop_art = parse_bishop_block(&stdout);

    let is_certificate = detect_certificate(&pub_name, &key_type);

    // Probe encryption status via empty-passphrase pubkey derivation.
    // Cert files have no encrypted-private-key counterpart, so skip.
    let encrypted = if is_certificate {
        false
    } else {
        private_key_encrypted(&private_path)
    };

    // Agent match by fingerprint (already SHA256-prefixed in both sides).
    let agent_loaded = agent_fingerprints.contains(&fingerprint);

    let strength_score = strength_score_for(&key_type, &bits, encrypted);

    let mtime_ts = file_mtime_ts(&private_path, pub_path);

    Some(SshKeyInfo {
        name,
        display_path,
        key_type,
        bits,
        fingerprint,
        comment,
        linked_hosts,
        bishop_art,
        strength_score,
        encrypted,
        agent_loaded,
        is_certificate,
        mtime_ts,
    })
}

/// File mtime of the private key, falling back to the pub key when the
/// private file is missing or unreadable. Returns seconds since UNIX
/// epoch. mtime is the most portable proxy for "key created" on Unix;
/// btime exists on some filesystems but Rust's stable std cannot read
/// it portably without a third-party crate.
fn file_mtime_ts(private_path: &Path, pub_path: &Path) -> Option<u64> {
    let from = |p: &Path| {
        std::fs::metadata(p)
            .ok()
            .and_then(|m| m.modified().ok())
            .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
            .map(|d| d.as_secs())
    };
    from(private_path).or_else(|| from(pub_path))
}

/// Detect whether a `.pub` file holds an OpenSSH user certificate.
///
/// Two paths trigger detection:
/// 1. Filename ends in `-cert.pub` (the convention `ssh-keygen -s` emits).
/// 2. `ssh-keygen -l` reported a cert variant in the type column, e.g.
///    `ED25519-CERT-V01@openssh.com`. This branch catches Vault SSH certs
///    and other signed pub keys the user renamed away from `-cert.pub`.
fn detect_certificate(pub_name: &str, key_type: &str) -> bool {
    pub_name.ends_with("-cert.pub") || key_type.to_ascii_lowercase().contains("-cert")
}

/// Parse ssh-keygen -lf output line into (bits, fingerprint, comment, type).
fn parse_keygen_output(line: &str) -> Option<(String, String, String, String)> {
    let parts: Vec<&str> = line.splitn(3, ' ').collect();
    if parts.len() < 3 {
        return None;
    }

    let bits = parts[0].to_string();
    let fingerprint = parts[1].to_string();

    // The rest is "<comment> (<type>)". Extract type from the end.
    let rest = parts[2];
    let (comment, key_type) = if let Some(paren_start) = rest.rfind('(') {
        let comment = rest[..paren_start].trim().to_string();
        let key_type = rest[paren_start + 1..].trim_end_matches(')').to_string();
        (comment, key_type)
    } else {
        (rest.to_string(), String::new())
    };

    Some((bits, fingerprint, comment, key_type))
}

/// Find host aliases that reference a given key path via IdentityFile.
/// Hosts without an explicit IdentityFile are linked to all keys (SSH tries them all).
fn find_linked_hosts(full_path: &Path, display_path: &str, hosts: &[HostEntry]) -> Vec<String> {
    // Only count explicit IdentityFile matches. SSH technically falls
    // back to trying every available key when no IdentityFile is set,
    // but rendering that as "this host is linked to every key" pollutes
    // every key's Linked Hosts grid with the same untargeted hosts.
    hosts
        .iter()
        .filter(|h| {
            if h.identity_file.is_empty() {
                return false;
            }
            h.identity_file == display_path || Path::new(&h.identity_file) == full_path
        })
        .map(|h| h.alias.clone())
        .collect()
}

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

    #[test]
    fn test_parse_keygen_output_ed25519() {
        let line = "256 SHA256:abcdef1234567890 user@host (ED25519)";
        let (bits, fp, comment, key_type) = parse_keygen_output(line).unwrap();
        assert_eq!(bits, "256");
        assert_eq!(fp, "SHA256:abcdef1234567890");
        assert_eq!(comment, "user@host");
        assert_eq!(key_type, "ED25519");
    }

    #[test]
    fn test_parse_keygen_output_rsa() {
        let line = "4096 SHA256:xyz9876543210 deploy@prod.example.com (RSA)";
        let (bits, fp, comment, key_type) = parse_keygen_output(line).unwrap();
        assert_eq!(bits, "4096");
        assert_eq!(fp, "SHA256:xyz9876543210");
        assert_eq!(comment, "deploy@prod.example.com");
        assert_eq!(key_type, "RSA");
    }

    #[test]
    fn test_parse_keygen_output_no_comment() {
        let line = "256 SHA256:fingerprint (ED25519)";
        let (bits, fp, comment, key_type) = parse_keygen_output(line).unwrap();
        assert_eq!(bits, "256");
        assert_eq!(fp, "SHA256:fingerprint");
        assert_eq!(comment, "");
        assert_eq!(key_type, "ED25519");
    }

    #[test]
    fn test_parse_keygen_output_comment_with_spaces() {
        let line = "256 SHA256:fingerprint eko@MacBook Pro (ED25519)";
        let (bits, fp, comment, key_type) = parse_keygen_output(line).unwrap();
        assert_eq!(bits, "256");
        assert_eq!(fp, "SHA256:fingerprint");
        assert_eq!(comment, "eko@MacBook Pro");
        assert_eq!(key_type, "ED25519");
    }

    #[test]
    fn test_parse_keygen_output_no_type_parens() {
        let line = "256 SHA256:fingerprint user@host";
        let (bits, fp, comment, key_type) = parse_keygen_output(line).unwrap();
        assert_eq!(bits, "256");
        assert_eq!(fp, "SHA256:fingerprint");
        assert_eq!(comment, "user@host");
        assert_eq!(key_type, "");
    }

    #[test]
    fn test_parse_keygen_output_too_short() {
        assert!(parse_keygen_output("256 SHA256:fp").is_none());
        assert!(parse_keygen_output("").is_none());
    }

    #[test]
    fn test_find_linked_hosts_display_path() {
        let hosts = vec![
            HostEntry {
                alias: "prod".to_string(),
                identity_file: "~/.ssh/id_ed25519".to_string(),
                ..Default::default()
            },
            HostEntry {
                alias: "staging".to_string(),
                identity_file: "~/.ssh/other_key".to_string(),
                ..Default::default()
            },
        ];
        let linked = find_linked_hosts(
            Path::new("/home/user/.ssh/id_ed25519"),
            "~/.ssh/id_ed25519",
            &hosts,
        );
        assert_eq!(linked, vec!["prod"]);
    }

    #[test]
    fn test_find_linked_hosts_full_path() {
        let hosts = vec![HostEntry {
            alias: "server".to_string(),
            identity_file: "/home/user/.ssh/deploy_key".to_string(),
            ..Default::default()
        }];
        let linked = find_linked_hosts(
            Path::new("/home/user/.ssh/deploy_key"),
            "~/.ssh/deploy_key",
            &hosts,
        );
        assert_eq!(linked, vec!["server"]);
    }

    #[test]
    fn test_find_linked_hosts_no_identity_file_does_not_link() {
        // Hosts without an explicit IdentityFile are excluded so the
        // Linked Hosts grid stays accurate per key instead of showing
        // every untargeted host under every key.
        let hosts = vec![HostEntry {
            alias: "server".to_string(),
            identity_file: String::new(),
            ..Default::default()
        }];
        let linked =
            find_linked_hosts(Path::new("/home/user/.ssh/id_rsa"), "~/.ssh/id_rsa", &hosts);
        assert!(linked.is_empty());
    }

    #[test]
    fn test_find_linked_hosts_wrong_identity_file() {
        let hosts = vec![HostEntry {
            alias: "server".to_string(),
            identity_file: "~/.ssh/other_key".to_string(),
            ..Default::default()
        }];
        let linked =
            find_linked_hosts(Path::new("/home/user/.ssh/id_rsa"), "~/.ssh/id_rsa", &hosts);
        assert!(linked.is_empty());
    }

    fn sample_key() -> SshKeyInfo {
        SshKeyInfo {
            name: "id_ed25519".to_string(),
            display_path: "~/.ssh/id_ed25519".to_string(),
            key_type: "ED25519".to_string(),
            bits: "256".to_string(),
            fingerprint: "SHA256:8x2k7HhPqQfvN5jJrUvWxTsXmnQ4LpBkEoYzNcAdGhI".to_string(),
            comment: "eric@MacBook".to_string(),
            linked_hosts: Vec::new(),
            bishop_art: String::new(),
            strength_score: 95,
            encrypted: true,
            agent_loaded: true,
            is_certificate: false,
            mtime_ts: None,
        }
    }

    #[test]
    fn test_type_display() {
        let key = sample_key();
        assert_eq!(key.type_display(), "ED25519 256");

        let key2 = SshKeyInfo {
            bits: String::new(),
            ..key
        };
        assert_eq!(key2.type_display(), "ED25519");
    }

    #[test]
    fn detect_certificate_via_filename_suffix() {
        assert!(detect_certificate("id_ed25519-cert.pub", "ED25519"));
    }

    #[test]
    fn detect_certificate_via_key_type_full_oid() {
        // ssh-keygen emits this form when a signed pub key is fed in even
        // though the filename omits the conventional `-cert.pub` suffix.
        assert!(detect_certificate(
            "id_ed25519-vault.pub",
            "ED25519-CERT-V01@openssh.com"
        ));
    }

    #[test]
    fn detect_certificate_via_key_type_short() {
        assert!(detect_certificate(
            "id_ed25519-breakglass.pub",
            "ED25519-CERT"
        ));
    }

    #[test]
    fn detect_certificate_rejects_plain_key() {
        assert!(!detect_certificate("id_ed25519.pub", "ED25519"));
    }

    #[test]
    fn detect_certificate_rejects_unrelated_dash_cert_in_name() {
        // A filename containing `cert` but not the `-cert.pub` suffix and a
        // non-cert key_type must not be flagged as a certificate.
        assert!(!detect_certificate("my-cert-backup.pub", "RSA"));
    }

    #[test]
    fn drunken_bishop_matches_openssh_canonical_17x9() {
        // Fingerprint generated with `ssh-keygen -t ed25519`; the bishop
        // block below is the exact `ssh-keygen -lv -E sha256` output for
        // that key. Locks the algorithm against OpenSSH's reference impl.
        let fp = decode_fingerprint("SHA256:1LayGj+CVIvJfOnQqADAT52DoJHhSa30feF/23wbRuE")
            .expect("decode fingerprint");
        let grid = drunken_bishop_grid(&fp, 17, 9);
        let rendered: Vec<String> = grid
            .iter()
            .map(|row| row.iter().map(|&c| bishop_char(c)).collect())
            .collect();
        assert_eq!(
            rendered,
            vec![
                "+=o o .          ",
                "*+.+ + . .       ",
                "+o= . o o o    . ",
                ".. o ..+ . .  . .",
                ".  o *.oS .    E ",
                ".   O =  + .  .  ",
                " . o =. . . +  o ",
                "  . . o+.  . o...",
                "      ....    ...",
            ]
        );
    }

    #[test]
    fn drunken_bishop_scales_to_larger_grid() {
        // At a larger grid the walk still starts at center and produces a
        // sparser pattern (same step count over more cells). Just sanity-
        // check that the dimensions match the request and that the center
        // tile carries the S marker as expected.
        let fp = decode_fingerprint("SHA256:1LayGj+CVIvJfOnQqADAT52DoJHhSa30feF/23wbRuE")
            .expect("decode fingerprint");
        let grid = drunken_bishop_grid(&fp, 25, 13);
        assert_eq!(grid.len(), 13);
        assert!(grid.iter().all(|row| row.len() == 25));
        assert_eq!(grid[6][12], BISHOP_S_INDEX);
    }

    #[test]
    fn decode_fingerprint_rejects_other_hash_prefixes() {
        assert!(decode_fingerprint("MD5:abcd").is_none());
        assert!(decode_fingerprint("plain-text").is_none());
    }

    #[test]
    fn test_bishop_lines_empty() {
        let key = SshKeyInfo {
            bishop_art: String::new(),
            ..sample_key()
        };
        assert!(key.bishop_lines().is_empty());
    }

    #[test]
    fn test_bishop_lines_split() {
        let key = SshKeyInfo {
            bishop_art: "+--[ED25519 256]--+\n|       .o*+      |\n+----[SHA256]-----+".to_string(),
            ..sample_key()
        };
        assert_eq!(key.bishop_lines().len(), 3);
        assert_eq!(key.bishop_lines()[1], "|       .o*+      |");
    }

    #[test]
    fn test_parse_agent_list_two_keys() {
        let stdout = "256 SHA256:abc1 eric@host (ED25519)\n4096 SHA256:def2 work@laptop (RSA)\n";
        let set = parse_agent_list(stdout);
        assert_eq!(set.len(), 2);
        assert!(set.contains("SHA256:abc1"));
        assert!(set.contains("SHA256:def2"));
    }

    #[test]
    fn test_parse_agent_list_empty_agent() {
        let stdout = "The agent has no identities.\n";
        let set = parse_agent_list(stdout);
        assert!(set.is_empty());
    }

    #[test]
    fn test_parse_agent_list_banner_skipped() {
        let stdout = "Could not open a connection to your authentication agent.\n";
        let set = parse_agent_list(stdout);
        assert!(set.is_empty());
    }

    #[test]
    fn test_strength_score_ed25519() {
        assert_eq!(strength_score_for("ED25519", "256", true), 95);
        assert_eq!(strength_score_for("ED25519", "256", false), 80);
    }

    #[test]
    fn test_strength_score_sk_ed25519() {
        assert_eq!(strength_score_for("sk-ED25519", "256", true), 100);
        assert_eq!(strength_score_for("sk-ED25519", "256", false), 85);
    }

    #[test]
    fn test_strength_score_rsa_buckets() {
        assert_eq!(strength_score_for("RSA", "1024", true), 20);
        assert_eq!(strength_score_for("RSA", "2048", true), 60);
        assert_eq!(strength_score_for("RSA", "3072", true), 80);
        assert_eq!(strength_score_for("RSA", "4096", true), 85);
        assert_eq!(strength_score_for("RSA", "8192", true), 85);
    }

    #[test]
    fn test_strength_score_dsa_is_low() {
        assert_eq!(strength_score_for("DSA", "1024", true), 10);
        assert_eq!(strength_score_for("DSA", "1024", false), 0);
    }

    #[test]
    fn test_strength_score_ecdsa_buckets() {
        assert_eq!(strength_score_for("ECDSA", "256", true), 75);
        assert_eq!(strength_score_for("ECDSA", "384", true), 85);
        assert_eq!(strength_score_for("ECDSA", "521", true), 90);
    }

    #[test]
    fn test_strength_score_unknown_type() {
        assert_eq!(strength_score_for("WEIRD", "256", true), 55);
        assert_eq!(strength_score_for("", "0", false), 40);
    }

    #[test]
    fn test_parse_bishop_block_typical_output() {
        let stdout = "\
256 SHA256:abc eric@host (ED25519)
+--[ED25519 256]--+
|                 |
|                 |
|      . .  . ... |
|       o o..ooo.o|
|      . S =.oo+==|
|     . o   B +E*B|
|      . . O =.=.+|
|     ..  = B o.oo|
|      .oo.+.=o.. |
+----[SHA256]-----+
";
        let art = parse_bishop_block(stdout);
        assert_eq!(art.lines().count(), 11);
        assert!(art.starts_with("+--[ED25519 256]--+"));
        assert!(art.ends_with("+----[SHA256]-----+"));
    }

    #[test]
    fn test_parse_bishop_block_missing_returns_empty() {
        let stdout = "256 SHA256:abc eric@host (ED25519)\n";
        assert!(parse_bishop_block(stdout).is_empty());
    }

    #[test]
    fn test_parse_bishop_block_truncated_returns_empty() {
        let stdout = "+--[ED25519 256]--+\n|   |\n+--+\n";
        assert!(parse_bishop_block(stdout).is_empty());
    }

    fn search_corpus() -> Vec<SshKeyInfo> {
        vec![
            SshKeyInfo {
                name: "id_ed25519".into(),
                comment: "eric@mac".into(),
                ..sample_key()
            },
            SshKeyInfo {
                name: "yubikey_work".into(),
                comment: "yubi@work".into(),
                ..sample_key()
            },
            SshKeyInfo {
                name: "customer-x".into(),
                comment: "eric@customer".into(),
                ..sample_key()
            },
        ]
    }

    #[test]
    fn filtered_key_indices_none_returns_all() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, None);
        assert_eq!(idx, vec![0, 1, 2]);
    }

    #[test]
    fn filtered_key_indices_empty_returns_all() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, Some(""));
        assert_eq!(idx, vec![0, 1, 2]);
    }

    #[test]
    fn filtered_key_indices_matches_name() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, Some("yubi"));
        assert_eq!(idx, vec![1]);
    }

    #[test]
    fn filtered_key_indices_matches_comment() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, Some("eric"));
        assert_eq!(idx, vec![0, 2]);
    }

    #[test]
    fn filtered_key_indices_case_insensitive() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, Some("ERIC"));
        assert_eq!(idx, vec![0, 2]);
    }

    #[test]
    fn filtered_key_indices_no_match() {
        let keys = search_corpus();
        let idx = filtered_key_indices(&keys, Some("nonexistent"));
        assert!(idx.is_empty());
    }

    #[test]
    fn resolve_selection_unfiltered_is_identity() {
        let keys = search_corpus();
        assert_eq!(resolve_selection(&keys, None, 0), Some(0));
        assert_eq!(resolve_selection(&keys, None, 2), Some(2));
        assert_eq!(resolve_selection(&keys, None, 99), None);
    }

    #[test]
    fn resolve_selection_filtered_maps_back_to_underlying() {
        let keys = search_corpus();
        // "eric" matches indices 0 (id_ed25519, eric@mac) and 2 (customer-x, eric@customer).
        assert_eq!(resolve_selection(&keys, Some("eric"), 0), Some(0));
        assert_eq!(resolve_selection(&keys, Some("eric"), 1), Some(2));
        assert_eq!(resolve_selection(&keys, Some("eric"), 2), None);
    }

    #[test]
    fn resolve_selection_no_match_returns_none() {
        let keys = search_corpus();
        assert_eq!(resolve_selection(&keys, Some("xyzzy"), 0), None);
    }

    #[cfg(unix)]
    fn read_only_entry(dir: &Path, name: &str) -> std::fs::DirEntry {
        std::fs::read_dir(dir)
            .expect("read_dir")
            .filter_map(Result::ok)
            .find(|e| e.file_name() == name)
            .expect("entry not found")
    }

    #[cfg(unix)]
    #[test]
    fn test_is_public_key_file_accepts_regular_pub_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("id_ed25519.pub");
        std::fs::write(&path, b"ssh-ed25519 AAAA").unwrap();
        let entry = read_only_entry(dir.path(), "id_ed25519.pub");
        assert!(is_public_key_file(&entry));
    }

    #[cfg(unix)]
    #[test]
    fn test_is_public_key_file_accepts_symlink_to_regular_pub_file() {
        use std::os::unix::fs::symlink;
        let target_dir = tempfile::tempdir().unwrap();
        let link_dir = tempfile::tempdir().unwrap();
        let target = target_dir.path().join("id_ed25519.pub");
        std::fs::write(&target, b"ssh-ed25519 AAAA").unwrap();
        let link = link_dir.path().join("id_ed25519.pub");
        symlink(&target, &link).unwrap();
        let entry = read_only_entry(link_dir.path(), "id_ed25519.pub");
        assert!(is_public_key_file(&entry));
    }

    #[cfg(unix)]
    #[test]
    fn test_is_public_key_file_rejects_broken_symlink() {
        use std::os::unix::fs::symlink;
        let dir = tempfile::tempdir().unwrap();
        let link = dir.path().join("id_ed25519.pub");
        symlink(dir.path().join("does_not_exist.pub"), &link).unwrap();
        let entry = read_only_entry(dir.path(), "id_ed25519.pub");
        assert!(!is_public_key_file(&entry));
    }

    #[cfg(unix)]
    #[test]
    fn test_is_public_key_file_rejects_symlink_to_directory() {
        use std::os::unix::fs::symlink;
        let dir = tempfile::tempdir().unwrap();
        let real_dir = dir.path().join("realdir");
        std::fs::create_dir(&real_dir).unwrap();
        let link = dir.path().join("id_ed25519.pub");
        symlink(&real_dir, &link).unwrap();
        let entry = read_only_entry(dir.path(), "id_ed25519.pub");
        assert!(!is_public_key_file(&entry));
    }

    // --- file_mtime_ts coverage (added during code review) ---

    #[test]
    fn file_created_ts_returns_private_key_mtime_when_present() {
        let dir = tempfile::tempdir().unwrap();
        let priv_path = dir.path().join("id_ed25519");
        let pub_path = dir.path().join("id_ed25519.pub");
        std::fs::write(&priv_path, b"PRIVATE").unwrap();
        std::fs::write(&pub_path, b"ssh-ed25519 AAAA").unwrap();
        let ts = file_mtime_ts(&priv_path, &pub_path).expect("private mtime");
        assert!(ts > 0);
    }

    #[test]
    fn file_created_ts_falls_back_to_pubkey_when_private_missing() {
        let dir = tempfile::tempdir().unwrap();
        let priv_path = dir.path().join("does_not_exist");
        let pub_path = dir.path().join("id_ed25519.pub");
        std::fs::write(&pub_path, b"ssh-ed25519 AAAA").unwrap();
        let ts = file_mtime_ts(&priv_path, &pub_path).expect("pubkey mtime");
        assert!(ts > 0);
    }

    #[test]
    fn file_created_ts_returns_none_when_both_missing() {
        let dir = tempfile::tempdir().unwrap();
        let priv_path = dir.path().join("nope_priv");
        let pub_path = dir.path().join("nope_pub.pub");
        assert!(file_mtime_ts(&priv_path, &pub_path).is_none());
    }
}