pub struct File { /* private fields */ }
Expand description
Representation of an OpenSSH-compatible known_hosts
file.
This file stores information about host keys. It is a list of entries, where each entry has a
hostname and a public key. The hostname can be represented in plaintext (example.com
), as a
plaintext pattern (*.examp?e.com
), as a list of such patterns (example.com,github.com
) or
in a hashed format that hides the hostname (|1|kRjF0OC...
).
You can iterate over all entries using entries()
, or you can use
match_hostname_key()
/match_host_port_key()
to lookup the entries that either accept or revoke a given combination of host and key.
You can also append new entries to the file using append_entry()
. In
this way, it is possible to update the known_hosts
file with new keys, without touching the
previous entries (all previous lines will be preserved verbatim, including comments and invalid
lines).
Implementations§
source§impl File
impl File
sourcepub fn decode(data: Bytes) -> Self
pub fn decode(data: Bytes) -> Self
Parses a file in OpenSSH known_hosts
format.
This function never fails: invalid lines are silently ignored.
sourcepub fn entries(&self) -> impl Iterator<Item = &Entry>
pub fn entries(&self) -> impl Iterator<Item = &Entry>
Iterates through all entries in the file.
Comments and invalid lines are not returned by this method.
sourcepub fn match_hostname_key(
&self,
hostname: &str,
pubkey: &Pubkey
) -> KeyMatch<'_>
pub fn match_hostname_key( &self, hostname: &str, pubkey: &Pubkey ) -> KeyMatch<'_>
Finds the match for the given hostname and key in this file.
See host_port_to_hostname()
for the format of the hostname
; you can use
match_host_port_key()
to match a (host, port)
pair.
If you want more advanced processing, you can use entries()
to list
all entries and the Entry::matches_hostname()
and Entry::pubkey()
methods to match
them to a hostname and key.
sourcepub fn match_host_port_key(
&self,
host: &str,
port: u16,
pubkey: &Pubkey
) -> KeyMatch<'_>
pub fn match_host_port_key( &self, host: &str, port: u16, pubkey: &Pubkey ) -> KeyMatch<'_>
Finds the match for the given host and port in this file.
Same as match_hostname_key()
, but formats the host and port
using host_port_to_hostname()
.
sourcepub fn entry_builder() -> EntryBuilder
pub fn entry_builder() -> EntryBuilder
Creates an EntryBuilder
, which can be used to add an entry (or a set of entries) to the
file (see append_entry()
).
sourcepub fn append_entry(&mut self, builder: &EntryBuilder)
pub fn append_entry(&mut self, builder: &EntryBuilder)
Appends all entries from the EntryBuilder
to this file.
sourcepub fn encode(&self) -> BytesMut
pub fn encode(&self) -> BytesMut
Encodes this file into an OpenSSH-compatible known_hosts
file.
If the file was created using decode()
, the original lines will be
preserved verbatim, including comments and invalid lines. Entries added using
append_entry()
will be appended as new lines at the end of the
file.