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:
- 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
][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
impl PublicKey
sourcepub fn new(key_data: KeyData, comment: impl Into<String>) -> Self
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.
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.com
sourcepub 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>
pub fn to_openssh(&self) -> Result<String>
Encode an OpenSSH-formatted public key, allocating a String
for
the result.
sourcepub fn read_openssh_file(path: &Path) -> Result<Self>
pub fn read_openssh_file(path: &Path) -> Result<Self>
Read public key from an OpenSSH-formatted file.
sourcepub fn write_openssh_file(&self, path: &Path) -> Result<()>
pub fn write_openssh_file(&self, path: &Path) -> Result<()>
Write public key as an OpenSSH-formatted file.
sourcepub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
pub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
Compute key fingerprint.
Use Default::default()
to use the default hash function (SHA-256).
sourcepub fn set_comment(&mut self, comment: impl Into<String>)
pub fn set_comment(&mut self, comment: impl Into<String>)
Set the comment on the key.
Trait Implementations§
source§impl From<&PrivateKey> for PublicKey
impl From<&PrivateKey> for PublicKey
source§fn from(private_key: &PrivateKey) -> PublicKey
fn from(private_key: &PrivateKey) -> PublicKey
source§impl From<DsaPublicKey> for PublicKey
impl From<DsaPublicKey> for PublicKey
source§fn from(public_key: DsaPublicKey) -> PublicKey
fn from(public_key: DsaPublicKey) -> PublicKey
source§impl From<EcdsaPublicKey> for PublicKey
impl From<EcdsaPublicKey> for PublicKey
source§fn from(public_key: EcdsaPublicKey) -> PublicKey
fn from(public_key: EcdsaPublicKey) -> PublicKey
source§impl From<Ed25519PublicKey> for PublicKey
impl From<Ed25519PublicKey> for PublicKey
source§fn from(public_key: Ed25519PublicKey) -> PublicKey
fn from(public_key: Ed25519PublicKey) -> PublicKey
source§impl From<PrivateKey> for PublicKey
impl From<PrivateKey> for PublicKey
source§fn from(private_key: PrivateKey) -> PublicKey
fn from(private_key: PrivateKey) -> PublicKey
source§impl From<RsaPublicKey> for PublicKey
impl From<RsaPublicKey> for PublicKey
source§fn from(public_key: RsaPublicKey) -> PublicKey
fn from(public_key: RsaPublicKey) -> PublicKey
source§impl From<SkEcdsaSha2NistP256> for PublicKey
impl From<SkEcdsaSha2NistP256> for PublicKey
source§fn from(public_key: SkEcdsaSha2NistP256) -> PublicKey
fn from(public_key: SkEcdsaSha2NistP256) -> PublicKey
source§impl Ord for PublicKey
impl Ord for PublicKey
source§impl PartialOrd for PublicKey
impl PartialOrd for PublicKey
impl Eq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations§
impl Freeze for PublicKey
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)