pub struct PrivateKey { /* private fields */ }Expand description
SSH private key.
Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn new(key_data: KeypairData, comment: impl Into<Comment>) -> Result<Self>
Available on crate feature alloc only.
pub fn new(key_data: KeypairData, comment: impl Into<Comment>) -> Result<Self>
alloc only.Create a new unencrypted private key with the given keypair data and comment.
On no_alloc platforms, use PrivateKey::try_from(key_data) instead.
§Errors
Returns Error::Encrypted if the key is encrypted.
Sourcepub fn from_openssh(pem: impl AsRef<[u8]>) -> Result<Self>
pub fn from_openssh(pem: impl AsRef<[u8]>) -> Result<Self>
Parse an OpenSSH-formatted PEM private key.
OpenSSH-formatted private keys begin with the following:
-----BEGIN OPENSSH PRIVATE KEY-----§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn from_ppk(
ppk: impl AsRef<str>,
passphrase: Option<String>,
) -> Result<Self>
Available on crate feature ppk only.
pub fn from_ppk( ppk: impl AsRef<str>, passphrase: Option<String>, ) -> Result<Self>
ppk only.Parse a PuTTY PPK private key.
PPK-formatted private keys begin with the following:
PuTTY-User-Key-File-<VERSION>: <ALGORITHM>§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Parse a raw binary SSH private key.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn encode_openssh<'o>(
&self,
line_ending: LineEnding,
out: &'o mut [u8],
) -> Result<&'o str>
pub fn encode_openssh<'o>( &self, line_ending: LineEnding, out: &'o mut [u8], ) -> Result<&'o str>
Encode OpenSSH-formatted (PEM) private key.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn to_openssh(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>
Available on crate feature alloc only.
pub fn to_openssh(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>
alloc only.Encode an OpenSSH-formatted PEM private key, allocating a self-zeroizing String for the
result.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn to_bytes(&self) -> Result<Zeroizing<Vec<u8>>>
Available on crate feature alloc only.
pub fn to_bytes(&self) -> Result<Zeroizing<Vec<u8>>>
alloc only.Serialize SSH private key as raw bytes.
§Errors
Returns Error::Encoding in the event of an encoding error.
Sourcepub fn sign(
&self,
namespace: &str,
hash_alg: HashAlg,
msg: &[u8],
) -> Result<SshSig>
Available on crate feature alloc only.
pub fn sign( &self, namespace: &str, hash_alg: HashAlg, msg: &[u8], ) -> Result<SshSig>
alloc only.Sign the given message using this private key, returning an SshSig.
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: PublicKey::verify.
use ssh_key::{PrivateKey, HashAlg, SshSig};
// Message to be signed.
let message = b"testing";
// Example domain/namespace used for the message.
let namespace = "example";
// Private key to use when computing the signature.
// WARNING: don't actually hardcode private keys in source code!!!
let encoded_private_key = r#"
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCzPq7zfqLffKoBDe/eo04kH2XxtSmk9D7RQyf1xUqrYgAAAJgAIAxdACAM
XQAAAAtzc2gtZWQyNTUxOQAAACCzPq7zfqLffKoBDe/eo04kH2XxtSmk9D7RQyf1xUqrYg
AAAEC2BsIi0QwW2uFscKTUUXNHLsYX4FxlaSDSblbAj7WR7bM+rvN+ot98qgEN796jTiQf
ZfG1KaT0PtFDJ/XFSqtiAAAAEHVzZXJAZXhhbXBsZS5jb20BAgMEBQ==
-----END OPENSSH PRIVATE KEY-----
"#;
let private_key = encoded_private_key.parse::<PrivateKey>()?;
let signature = private_key.sign(namespace, HashAlg::default(), message)?;
// assert!(private_key.public_key().verify(namespace, message, &signature).is_ok());§Errors
Propagates errors from SshSig::sign.
Sourcepub fn sign_digest<D: AssociatedHashAlg + Digest>(
&self,
namespace: &str,
digest: D,
) -> Result<SshSig>
Available on crate feature alloc only.
pub fn sign_digest<D: AssociatedHashAlg + Digest>( &self, namespace: &str, digest: D, ) -> Result<SshSig>
alloc only.Sign the given message Digest using this private key, returning an SshSig.
These signatures can be produced using ssh-keygen -Y sign.
For more information, see PrivateKey::sign.
§Errors
Propagates errors from SshSig::sign_digest.
Sourcepub fn sign_prehash(
&self,
namespace: &str,
hash_alg: HashAlg,
prehash: &[u8],
) -> Result<SshSig>
Available on crate feature alloc only.
pub fn sign_prehash( &self, namespace: &str, hash_alg: HashAlg, prehash: &[u8], ) -> Result<SshSig>
alloc only.Sign the given raw message prehash using this private key, returning an SshSig.
These signatures can be produced using ssh-keygen -Y sign.
For more information, see PrivateKey::sign.
§Errors
Propagates errors from SshSig::sign_prehash.
Sourcepub fn read_openssh(reader: &mut impl Read) -> Result<Self>
Available on crate feature std only.
pub fn read_openssh(reader: &mut impl Read) -> Result<Self>
std only.Read private key from an OpenSSH-formatted PEM source.
§Errors
- Returns
Error::Ioon I/O errors. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn read_openssh_file(path: impl AsRef<Path>) -> Result<Self>
Available on crate feature std only.
pub fn read_openssh_file(path: impl AsRef<Path>) -> Result<Self>
std only.Read private key from an OpenSSH-formatted PEM file.
§Errors
- Returns
Error::Ioon I/O errors. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn write_openssh(
&self,
writer: &mut impl Write,
line_ending: LineEnding,
) -> Result<()>
Available on crate feature std only.
pub fn write_openssh( &self, writer: &mut impl Write, line_ending: LineEnding, ) -> Result<()>
std only.Write private key as an OpenSSH-formatted PEM file.
§Errors
- Returns
Error::Ioon I/O errors. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn write_openssh_file(
&self,
path: impl AsRef<Path>,
line_ending: LineEnding,
) -> Result<()>
Available on crate feature std only.
pub fn write_openssh_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<()>
std only.Write private key as an OpenSSH-formatted PEM file.
§Errors
- Returns
Error::Ioon I/O errors. - Returns
Error::Encodingin the event of an encoding error.
Sourcepub fn decrypt(&self, password: impl AsRef<[u8]>) -> Result<Self>
Available on crate feature encryption only.
pub fn decrypt(&self, password: impl AsRef<[u8]>) -> Result<Self>
encryption only.Attempt to decrypt an encrypted private key using the provided password to derive an encryption key.
§Errors
Returns Error::Decrypted if the private key is already decrypted.
Sourcepub fn encrypt<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
password: impl AsRef<[u8]>,
) -> Result<Self>
Available on crate feature encryption only.
pub fn encrypt<R: TryCryptoRng + ?Sized>( &self, rng: &mut R, password: impl AsRef<[u8]>, ) -> Result<Self>
encryption only.Encrypt an unencrypted private key using the provided password to derive an encryption key.
Uses the following algorithms:
- Cipher:
Cipher::Aes256Ctr - KDF:
Kdf::Bcrypt(i.e.bcrypt-pbkdf)
§Errors
Returns Error::Encrypted if the private key is already encrypted.
Sourcepub fn encrypt_with_cipher<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
cipher: Cipher,
password: impl AsRef<[u8]>,
) -> Result<Self>
Available on crate feature encryption only.
pub fn encrypt_with_cipher<R: TryCryptoRng + ?Sized>( &self, rng: &mut R, cipher: Cipher, password: impl AsRef<[u8]>, ) -> Result<Self>
encryption only.Encrypt an unencrypted private key using the provided password to
derive an encryption key for the provided Cipher.
§Errors
Returns Error::Encrypted if the private key is already encrypted.
Sourcepub fn encrypt_with(
&self,
cipher: Cipher,
kdf: Kdf,
checkint: u32,
password: impl AsRef<[u8]>,
) -> Result<Self>
Available on crate feature encryption only.
pub fn encrypt_with( &self, cipher: Cipher, kdf: Kdf, checkint: u32, password: impl AsRef<[u8]>, ) -> Result<Self>
encryption only.Encrypt an unencrypted private key using the provided cipher and KDF configuration.
§Errors
Returns Error::Encrypted if the private key is already encrypted.
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 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 is_encrypted(&self) -> bool
pub fn is_encrypted(&self) -> bool
Is this key encrypted?
Sourcepub fn kdf(&self) -> &Kdf
pub fn kdf(&self) -> &Kdf
Key Derivation Function (KDF) used to encrypt this key.
Returns Kdf::None if this key is not encrypted.
Sourcepub fn key_data(&self) -> &KeypairData
pub fn key_data(&self) -> &KeypairData
Keypair data.
Sourcepub fn public_key(&self) -> &PublicKey
pub fn public_key(&self) -> &PublicKey
Get the PublicKey which corresponds to this private key.
Sourcepub fn random<R: CryptoRng + ?Sized>(
rng: &mut R,
algorithm: Algorithm,
) -> Result<Self>
Available on crate feature rand_core only.
pub fn random<R: CryptoRng + ?Sized>( rng: &mut R, algorithm: Algorithm, ) -> Result<Self>
rand_core only.Generate a random key which uses the given algorithm.
§Errors
Returns Error::AlgorithmUnknown if the algorithm is unsupported.
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§
Source§impl Clone for PrivateKey
impl Clone for PrivateKey
Source§fn clone(&self) -> PrivateKey
fn clone(&self) -> PrivateKey
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl CtEq for PrivateKey
impl CtEq for PrivateKey
Source§impl Debug for PrivateKey
impl Debug for PrivateKey
Source§impl Decode for PrivateKey
impl Decode for PrivateKey
Source§impl Encode for PrivateKey
impl Encode for PrivateKey
Source§fn encoded_len(&self) -> Result<usize>
fn encoded_len(&self) -> Result<usize>
Source§fn encoded_len_prefixed(&self) -> Result<usize, Error>
fn encoded_len_prefixed(&self) -> Result<usize, Error>
uint32 length prefix. Read moreSource§impl From<&PrivateKey> for KeyData
impl From<&PrivateKey> for KeyData
Source§fn from(private_key: &PrivateKey) -> KeyData
fn from(private_key: &PrivateKey) -> KeyData
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<DsaKeypair> for PrivateKey
Available on crate feature alloc only.
impl From<DsaKeypair> for PrivateKey
alloc only.Source§fn from(keypair: DsaKeypair) -> PrivateKey
fn from(keypair: DsaKeypair) -> PrivateKey
Source§impl From<EcdsaKeypair> for PrivateKey
Available on crate feature ecdsa only.
impl From<EcdsaKeypair> for PrivateKey
ecdsa only.Source§fn from(keypair: EcdsaKeypair) -> PrivateKey
fn from(keypair: EcdsaKeypair) -> PrivateKey
Source§impl From<Ed25519Keypair> for PrivateKey
impl From<Ed25519Keypair> for PrivateKey
Source§fn from(keypair: Ed25519Keypair) -> PrivateKey
fn from(keypair: Ed25519Keypair) -> PrivateKey
Source§impl From<PrivateKey> for KeyData
impl From<PrivateKey> for KeyData
Source§fn from(private_key: PrivateKey) -> KeyData
fn from(private_key: PrivateKey) -> KeyData
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<RsaKeypair> for PrivateKey
Available on crate feature alloc only.
impl From<RsaKeypair> for PrivateKey
alloc only.Source§fn from(keypair: RsaKeypair) -> PrivateKey
fn from(keypair: RsaKeypair) -> PrivateKey
Source§impl From<SkEcdsaSha2NistP256> for PrivateKey
Available on crate features alloc and ecdsa only.
impl From<SkEcdsaSha2NistP256> for PrivateKey
alloc and ecdsa only.Source§fn from(keypair: SkEcdsaSha2NistP256) -> PrivateKey
fn from(keypair: SkEcdsaSha2NistP256) -> PrivateKey
Source§impl From<SkEd25519> for PrivateKey
Available on crate feature alloc only.
impl From<SkEd25519> for PrivateKey
alloc only.Source§fn from(keypair: SkEd25519) -> PrivateKey
fn from(keypair: SkEd25519) -> PrivateKey
Source§impl FromStr for PrivateKey
impl FromStr for PrivateKey
Source§impl PartialEq for PrivateKey
impl PartialEq for PrivateKey
Source§impl PemLabel for PrivateKey
impl PemLabel for PrivateKey
Source§impl Signer<Signature> for PrivateKey
Available on crate feature alloc only.
impl Signer<Signature> for PrivateKey
alloc only.Source§impl TryFrom<KeypairData> for PrivateKey
impl TryFrom<KeypairData> for PrivateKey
Source§fn try_from(key_data: KeypairData) -> Result<PrivateKey>
fn try_from(key_data: KeypairData) -> Result<PrivateKey>
impl Eq for PrivateKey
Auto Trait Implementations§
impl Freeze for PrivateKey
impl RefUnwindSafe for PrivateKey
impl Send for PrivateKey
impl Sync for PrivateKey
impl Unpin for PrivateKey
impl UnsafeUnpin for PrivateKey
impl UnwindSafe for PrivateKey
Blanket Implementations§
Source§impl<S, T> AsyncSigner<S> for Twhere
T: Signer<S>,
impl<S, T> AsyncSigner<S> for Twhere
T: Signer<S>,
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<T> EncodePem for T
impl<T> EncodePem for T
Source§fn encode_pem<'o>(
&self,
line_ending: LineEnding,
out: &'o mut [u8],
) -> Result<&'o str, Error>
fn encode_pem<'o>( &self, line_ending: LineEnding, out: &'o mut [u8], ) -> Result<&'o str, Error>
Source§fn encode_pem_string(&self, line_ending: LineEnding) -> Result<String, Error>
fn encode_pem_string(&self, line_ending: LineEnding) -> Result<String, Error>
alloc only.Source§impl<T> SigningKey for T
impl<T> SigningKey for T
Source§fn public_key(&self) -> KeyData
fn public_key(&self) -> KeyData
alloc only.public::KeyData for this signing key.