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.
Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub fn new(key_data: KeyData, comment: impl Into<String>) -> PublicKey
pub fn new(key_data: KeyData, comment: impl Into<String>) -> PublicKey
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.comSourcepub 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.
Sourcepub fn to_openssh(&self) -> Result<String, Error>
pub fn to_openssh(&self) -> Result<String, Error>
Encode an OpenSSH-formatted public key, allocating a String for
the result.
Sourcepub fn verify(
&self,
namespace: &str,
msg: &[u8],
signature: &SshSig,
) -> Result<(), Error>
pub fn verify( &self, namespace: &str, msg: &[u8], signature: &SshSig, ) -> Result<(), Error>
Verify the SshSig signature over the given message using this
public key.
These signatures can be produced using ssh-keygen -Y sign. They’re
encoded as PEM and begin with the following:
-----BEGIN SSH SIGNATURE-----See PROTOCOL.sshsig for more information.
§Usage
See also: [PrivateKey::sign].
use ssh_key::{PublicKey, SshSig};
// Message to be verified.
let message = b"testing";
// Example domain/namespace used for the message.
let namespace = "example";
// Public key which computed the signature.
let encoded_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com";
// Example signature to be verified.
let signature_str = r#"
-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgsz6u836i33yqAQ3v3qNOJB9l8b
UppPQ+0UMn9cVKq2IAAAAHZXhhbXBsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtzc2gtZWQy
NTUxOQAAAEBPEav+tMGNnox4MuzM7rlHyVBajCn8B0kAyiOWwPKprNsG3i6X+voz/WCSik
/FowYwqhgCABUJSvRX3AERVBUP
-----END SSH SIGNATURE-----
"#;
let public_key = encoded_public_key.parse::<PublicKey>()?;
let signature = signature_str.parse::<SshSig>()?;
public_key.verify(namespace, message, &signature)?;Sourcepub fn read_openssh_file(path: &Path) -> Result<PublicKey, Error>
pub fn read_openssh_file(path: &Path) -> Result<PublicKey, Error>
Read public key from an OpenSSH-formatted file.
Sourcepub fn write_openssh_file(&self, path: &Path) -> Result<(), Error>
pub fn write_openssh_file(&self, path: &Path) -> Result<(), Error>
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
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<EcdsaPublicKey> for PublicKey
Available on crate feature ecdsa only.
impl From<EcdsaPublicKey> for PublicKey
ecdsa only.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
Available on crate feature alloc only.
impl From<RsaPublicKey> for PublicKey
alloc only.Source§fn from(public_key: RsaPublicKey) -> PublicKey
fn from(public_key: RsaPublicKey) -> PublicKey
Source§impl From<SkEcdsaSha2NistP256> for PublicKey
Available on crate feature ecdsa only.
impl From<SkEcdsaSha2NistP256> for PublicKey
ecdsa only.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§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request