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][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.
Note that since the comment is an artifact on the string serialization of
a public key, it will be implicitly dropped when encoding as a binary
format. To ensure it’s always preserved even when using binary formats, you
will first need to convert the PublicKey to a string using e.g.
PublicKey::to_openssh.
Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub fn new(key_data: KeyData, comment: impl Into<Comment>) -> PublicKey
Available on crate feature alloc only.
pub fn new(key_data: KeyData, comment: impl Into<Comment>) -> PublicKey
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<PublicKey, Error>
pub fn from_openssh(public_key: &str) -> Result<PublicKey, Error>
Parse an OpenSSH-formatted public key.
OpenSSH-formatted public keys look like the following:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti foo@bar.com§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, Error>
Parse a raw binary SSH public key.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str, Error>
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str, Error>
Encode OpenSSH-formatted public key.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn to_openssh(&self) -> Result<String, Error>
Available on crate feature alloc only.
pub fn to_openssh(&self) -> Result<String, Error>
alloc only.Encode an OpenSSH-formatted public key, allocating a String for the result.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, Error>
Available on crate feature alloc only.
pub fn to_bytes(&self) -> Result<Vec<u8>, Error>
alloc only.Serialize SSH public key as raw bytes.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn verify_digest<D>(
&self,
namespace: &str,
digest: D,
signature: &SshSig,
) -> Result<(), Error>where
D: AssociatedHashAlg + Digest,
Available on crate feature alloc only.
pub fn verify_digest<D>(
&self,
namespace: &str,
digest: D,
signature: &SshSig,
) -> Result<(), Error>where
D: AssociatedHashAlg + Digest,
alloc only.Verify the SshSig signature is valid the given message Digest using this public key.
See [PublicKey::verify] for more information.
§Errors
- Returns
Error::PublicKeyif this key does not match the one in the signature. - Returns
Error::Namespaceif the providednamespacedoes not match the signature. - Returns
Error::Signaturein the event signature verification failed.
Sourcepub fn verify_prehash(
&self,
namespace: &str,
prehash: &[u8],
signature: &SshSig,
) -> Result<(), Error>
Available on crate feature alloc only.
pub fn verify_prehash( &self, namespace: &str, prehash: &[u8], signature: &SshSig, ) -> Result<(), Error>
alloc only.Verify the SshSig signature matches the given prehashed message digest using this
public key.
See [PublicKey::verify] for more information.
§Errors
- Returns
Error::PublicKeyif this key does not match the one in the signature. - Returns
Error::Namespaceif the providednamespacedoes not match the signature. - Returns
Error::Signaturein the event signature verification failed.
Sourcepub fn read_openssh(reader: &mut impl Read) -> Result<PublicKey, Error>
Available on crate feature std only.
pub fn read_openssh(reader: &mut impl Read) -> Result<PublicKey, Error>
std only.Read public key from an OpenSSH-formatted source.
§Errors
- Returns
Error::Ioin the event of an I/O error. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn read_openssh_file(path: impl AsRef<Path>) -> Result<PublicKey, Error>
Available on crate feature std only.
pub fn read_openssh_file(path: impl AsRef<Path>) -> Result<PublicKey, Error>
std only.Read public key from an OpenSSH-formatted file.
§Errors
- Returns
Error::Ioin the event of an I/O error. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn write_openssh(&self, writer: &mut impl Write) -> Result<(), Error>
Available on crate feature std only.
pub fn write_openssh(&self, writer: &mut impl Write) -> Result<(), Error>
std only.Write public key as an OpenSSH-formatted file.
§Errors
- Returns
Error::Ioin the event of an I/O error. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn write_openssh_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
Available on crate feature std only.
pub fn write_openssh_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
std only.Write public key as an OpenSSH-formatted file.
§Errors
- Returns
Error::Ioin the event of an I/O error. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn comment(&self) -> &Comment
Available on crate feature alloc only.
pub fn comment(&self) -> &Comment
alloc only.Comment on the key (e.g. email address).
Sourcepub fn set_comment(&mut self, comment: impl Into<Comment>)
Available on crate feature alloc only.
pub fn set_comment(&mut self, comment: impl Into<Comment>)
alloc only.Set the comment on the key.
Trait Implementations§
impl Eq for 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<DsaPublicKey> for PublicKey
Available on crate feature alloc only.
impl From<DsaPublicKey> for PublicKey
alloc only.Source§fn from(public_key: DsaPublicKey) -> PublicKey
fn from(public_key: DsaPublicKey) -> 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
Available on crate feature alloc only.
impl From<RsaPublicKey> for PublicKey
alloc only.