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.comIt consists of the following three parts:
- Algorithm identifier (in this example
ssh-ed25519) - Key data encoded as Base64
- 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 and 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
sourceimpl PublicKey
impl PublicKey
sourcepub fn new(key_data: KeyData, comment: impl Into<String>) -> Self
Available on crate feature alloc only.
pub fn new(key_data: KeyData, comment: impl Into<String>) -> Self
alloc only.Create a new public key with the given comment.
On no_std platforms, use PublicKey::from(key_data) instead.
sourcepub fn from_openssh(public_key: &str) -> Result<Self>
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.comsourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Parse a raw binary SSH public key.
sourcepub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str>
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str>
Encode OpenSSH-formatted public key.
sourcepub fn to_openssh(&self) -> Result<String>
Available on crate feature alloc only.
pub fn to_openssh(&self) -> Result<String>
alloc only.Encode an OpenSSH-formatted public key, allocating a String for
the result.
sourcepub fn to_bytes(&self) -> Result<Vec<u8>>
Available on crate feature alloc only.
pub fn to_bytes(&self) -> Result<Vec<u8>>
alloc only.Serialize SSH public key as raw bytes.
sourcepub fn read_openssh_file(path: &Path) -> Result<Self>
Available on crate feature std only.
pub fn read_openssh_file(path: &Path) -> Result<Self>
std only.Read public key from an OpenSSH-formatted file.
sourcepub fn write_openssh_file(&self, path: &Path) -> Result<()>
Available on crate feature std only.
pub fn write_openssh_file(&self, path: &Path) -> Result<()>
std only.Write public key as an OpenSSH-formatted file.
sourcepub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
Available on crate feature fingerprint only.
pub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
fingerprint only.Compute key fingerprint.
Use Default::default() to use the default hash function (SHA-256).
sourcepub fn set_comment(&mut self, comment: impl Into<String>)
Available on crate feature alloc only.
pub fn set_comment(&mut self, comment: impl Into<String>)
alloc only.Set the comment on the key.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for PublicKey
Available on crate features alloc and serde only.
impl<'de> Deserialize<'de> for PublicKey
alloc and serde only.sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
sourceimpl From<&PrivateKey> for PublicKey
impl From<&PrivateKey> for PublicKey
sourcefn from(private_key: &PrivateKey) -> PublicKey
fn from(private_key: &PrivateKey) -> PublicKey
sourceimpl From<DsaPublicKey> for PublicKey
Available on crate feature alloc only.
impl From<DsaPublicKey> for PublicKey
alloc only.sourcefn from(public_key: DsaPublicKey) -> PublicKey
fn from(public_key: DsaPublicKey) -> PublicKey
sourceimpl From<EcdsaPublicKey> for PublicKey
Available on crate feature ecdsa only.
impl From<EcdsaPublicKey> for PublicKey
ecdsa only.sourcefn from(public_key: EcdsaPublicKey) -> PublicKey
fn from(public_key: EcdsaPublicKey) -> PublicKey
sourceimpl From<Ed25519PublicKey> for PublicKey
impl From<Ed25519PublicKey> for PublicKey
sourcefn from(public_key: Ed25519PublicKey) -> PublicKey
fn from(public_key: Ed25519PublicKey) -> PublicKey
sourceimpl From<PrivateKey> for PublicKey
impl From<PrivateKey> for PublicKey
sourcefn from(private_key: PrivateKey) -> PublicKey
fn from(private_key: PrivateKey) -> PublicKey
sourceimpl From<RsaPublicKey> for PublicKey
Available on crate feature alloc only.
impl From<RsaPublicKey> for PublicKey
alloc only.sourcefn from(public_key: RsaPublicKey) -> PublicKey
fn from(public_key: RsaPublicKey) -> PublicKey
sourceimpl From<SkEcdsaSha2NistP256> for PublicKey
Available on crate feature ecdsa only.
impl From<SkEcdsaSha2NistP256> for PublicKey
ecdsa only.sourcefn from(public_key: SkEcdsaSha2NistP256) -> PublicKey
fn from(public_key: SkEcdsaSha2NistP256) -> PublicKey
sourceimpl Ord for PublicKey
impl Ord for PublicKey
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl PartialOrd<PublicKey> for PublicKey
impl PartialOrd<PublicKey> for PublicKey
sourcefn partial_cmp(&self, other: &PublicKey) -> Option<Ordering>
fn partial_cmp(&self, other: &PublicKey) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more