Struct File

Source
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

Source

pub fn decode(data: Bytes) -> Self

Parses a file in OpenSSH known_hosts format.

This function never fails: invalid lines are silently ignored.

Source

pub fn new() -> Self

Creates a new empty File.

Source

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.

Source

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.

Source

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().

Source

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()).

Source

pub fn append_entry(&mut self, builder: &EntryBuilder)

Appends all entries from the EntryBuilder to this file.

Source

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.

Trait Implementations§

Source§

impl Clone for File

Source§

fn clone(&self) -> File

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for File

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for File

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V