Struct ssh_key::public::PublicKey

source ·
pub struct PublicKey { /* private fields */ }
Expand description

SSH public key.

§OpenSSH encoding

The OpenSSH encoding of an SSH public key looks like following:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com

It consists of the following three parts:

  1. Algorithm identifier (in this example ssh-ed25519)
  2. Key data encoded as Base64
  3. Comment (optional): arbitrary label describing a key. Usually an email address

The PublicKey::from_openssh and PublicKey::to_openssh methods can be used to decode/encode public keys, or alternatively, the FromStr and ToString impls.

§serde support

When the serde feature of this crate is enabled, this type receives impls of [Deserialize][serde::Deserialize] and [Serialize][serde::Serialize].

The serialization uses a binary encoding with binary formats like bincode and CBOR, and the OpenSSH string serialization when used with human-readable formats like JSON and TOML.

Implementations§

source§

impl PublicKey

source

pub fn new(key_data: KeyData, comment: impl Into<String>) -> Self

Create a new public key with the given comment.

On no_std platforms, use PublicKey::from(key_data) instead.

source

pub fn from_openssh(public_key: &str) -> Result<Self>

Parse an OpenSSH-formatted public key.

OpenSSH-formatted public keys look like the following:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti foo@bar.com
source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Parse a raw binary SSH public key.

source

pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str>

Encode OpenSSH-formatted public key.

source

pub fn to_openssh(&self) -> Result<String>

Encode an OpenSSH-formatted public key, allocating a String for the result.

source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Serialize SSH public key as raw bytes.

source

pub fn read_openssh_file(path: &Path) -> Result<Self>

Read public key from an OpenSSH-formatted file.

source

pub fn write_openssh_file(&self, path: &Path) -> Result<()>

Write public key as an OpenSSH-formatted file.

source

pub fn algorithm(&self) -> Algorithm

Get the digital signature Algorithm used by this key.

source

pub fn comment(&self) -> &str

Comment on the key (e.g. email address).

source

pub fn key_data(&self) -> &KeyData

Private key data.

source

pub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint

Compute key fingerprint.

Use Default::default() to use the default hash function (SHA-256).

source

pub fn set_comment(&mut self, comment: impl Into<String>)

Set the comment on the key.

Trait Implementations§

source§

impl Clone for PublicKey

source§

fn clone(&self) -> PublicKey

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 PublicKey

source§

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

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

impl From<&PrivateKey> for PublicKey

source§

fn from(private_key: &PrivateKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<&PublicKey> for KeyData

source§

fn from(public_key: &PublicKey) -> KeyData

Converts to this type from the input type.
source§

impl From<DsaPublicKey> for PublicKey

source§

fn from(public_key: DsaPublicKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<EcdsaPublicKey> for PublicKey

source§

fn from(public_key: EcdsaPublicKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<Ed25519PublicKey> for PublicKey

source§

fn from(public_key: Ed25519PublicKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<Entry> for PublicKey

source§

fn from(entry: Entry) -> PublicKey

Converts to this type from the input type.
source§

impl From<Entry> for PublicKey

source§

fn from(entry: Entry) -> PublicKey

Converts to this type from the input type.
source§

impl From<KeyData> for PublicKey

source§

fn from(key_data: KeyData) -> PublicKey

Converts to this type from the input type.
source§

impl From<PrivateKey> for PublicKey

source§

fn from(private_key: PrivateKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<PublicKey> for Entry

source§

fn from(public_key: PublicKey) -> Entry

Converts to this type from the input type.
source§

impl From<PublicKey> for KeyData

source§

fn from(public_key: PublicKey) -> KeyData

Converts to this type from the input type.
source§

impl From<RsaPublicKey> for PublicKey

source§

fn from(public_key: RsaPublicKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<SkEcdsaSha2NistP256> for PublicKey

source§

fn from(public_key: SkEcdsaSha2NistP256) -> PublicKey

Converts to this type from the input type.
source§

impl From<SkEd25519> for PublicKey

source§

fn from(public_key: SkEd25519) -> PublicKey

Converts to this type from the input type.
source§

impl FromStr for PublicKey

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Ord for PublicKey

source§

fn cmp(&self, other: &PublicKey) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for PublicKey

source§

fn eq(&self, other: &PublicKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for PublicKey

source§

fn partial_cmp(&self, other: &PublicKey) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ToString for PublicKey

source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl Verifier<Signature> for PublicKey

source§

fn verify(&self, message: &[u8], signature: &Signature) -> Result<()>

Use Self to verify that the provided signature for a given message bytestring is authentic. Read more
source§

impl Eq for PublicKey

source§

impl StructuralPartialEq for PublicKey

Auto Trait Implementations§

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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>,

§

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.