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.
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
pub fn new(key_data: KeyData, comment: impl Into<Comment>) -> 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.com
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.
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<Comment>)
pub fn set_comment(&mut self, comment: impl Into<Comment>)
Set the comment on the key.