Struct ssh_key::private::PrivateKey
source · [−]pub struct PrivateKey { /* private fields */ }
Expand description
SSH private key.
Implementations
sourceimpl PrivateKey
impl PrivateKey
sourcepub fn new(key_data: KeypairData, comment: impl Into<String>) -> Result<Self>
This is supported on crate feature alloc
only.
pub fn new(key_data: KeypairData, comment: impl Into<String>) -> Result<Self>
alloc
only.Create a new unencrypted private key with the given keypair data and comment.
On no_std
platforms, use PrivateKey::from(key_data)
instead.
sourcepub fn from_openssh(input: impl AsRef<[u8]>) -> Result<Self>
pub fn from_openssh(input: impl AsRef<[u8]>) -> Result<Self>
Parse an OpenSSH-formatted PEM private key.
OpenSSH-formatted private keys begin with the following:
-----BEGIN OPENSSH PRIVATE KEY-----
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.
sourcepub fn to_openssh(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>
This is supported 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.
sourcepub fn to_bytes(&self) -> Result<Zeroizing<Vec<u8>>>
This is supported on crate feature alloc
only.
pub fn to_bytes(&self) -> Result<Zeroizing<Vec<u8>>>
alloc
only.Serialize SSH private key as raw bytes.
sourcepub fn read_openssh_file(path: &Path) -> Result<Self>
This is supported on crate feature std
only.
pub fn read_openssh_file(path: &Path) -> Result<Self>
std
only.Read private key from an OpenSSH-formatted PEM file.
sourcepub fn write_openssh_file(
&self,
path: &Path,
line_ending: LineEnding
) -> Result<()>
This is supported on crate feature std
only.
pub fn write_openssh_file(
&self,
path: &Path,
line_ending: LineEnding
) -> Result<()>
std
only.Write private key as an OpenSSH-formatted PEM file.
sourcepub fn decrypt(&self, password: impl AsRef<[u8]>) -> Result<Self>
This is supported 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.
Returns Error::Decrypted
if the private key is already decrypted.
sourcepub fn encrypt(
&self,
rng: impl CryptoRng + RngCore,
password: impl AsRef<[u8]>
) -> Result<Self>
This is supported on crate feature encryption
only.
pub fn encrypt(
&self,
rng: impl CryptoRng + RngCore,
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
)
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>
This is supported 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.
Returns Error::Encrypted
if the private key is already encrypted.
sourcepub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
This is supported on crate feature fingerprint
only.
pub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint
fingerprint
only.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(
rng: impl CryptoRng + RngCore,
algorithm: Algorithm
) -> Result<Self>
This is supported on crate feature rand_core
only.
pub fn random(
rng: impl CryptoRng + RngCore,
algorithm: Algorithm
) -> Result<Self>
rand_core
only.Generate a random key which uses the given algorithm.
Returns
Error::Algorithm
if the algorithm is unsupported.
sourcepub fn set_comment(&mut self, comment: impl Into<String>)
This is supported on crate feature alloc
only.
pub fn set_comment(&mut self, comment: impl Into<String>)
alloc
only.Set the comment on the key.
Trait Implementations
sourceimpl Clone for PrivateKey
impl Clone for PrivateKey
sourcefn clone(&self) -> PrivateKey
fn clone(&self) -> PrivateKey
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl ConstantTimeEq for PrivateKey
This is supported on crate feature subtle
only.
impl ConstantTimeEq for PrivateKey
subtle
only.sourceimpl Debug for PrivateKey
impl Debug for PrivateKey
sourceimpl From<&'_ PrivateKey> for PublicKey
impl From<&'_ PrivateKey> for PublicKey
sourcefn from(private_key: &PrivateKey) -> PublicKey
fn from(private_key: &PrivateKey) -> PublicKey
Converts to this type from the input type.
sourceimpl From<&'_ PrivateKey> for KeyData
impl From<&'_ PrivateKey> for KeyData
sourcefn from(private_key: &PrivateKey) -> KeyData
fn from(private_key: &PrivateKey) -> KeyData
Converts to this type from the input type.
sourceimpl From<PrivateKey> for PublicKey
impl From<PrivateKey> for PublicKey
sourcefn from(private_key: PrivateKey) -> PublicKey
fn from(private_key: PrivateKey) -> PublicKey
Converts to this type from the input type.
sourceimpl From<PrivateKey> for KeyData
impl From<PrivateKey> for KeyData
sourcefn from(private_key: PrivateKey) -> KeyData
fn from(private_key: PrivateKey) -> KeyData
Converts to this type from the input type.
sourceimpl FromStr for PrivateKey
impl FromStr for PrivateKey
sourceimpl PartialEq<PrivateKey> for PrivateKey
This is supported on crate feature subtle
only.
impl PartialEq<PrivateKey> for PrivateKey
subtle
only.sourceimpl PemLabel for PrivateKey
impl PemLabel for PrivateKey
sourceimpl Signer<Signature> for PrivateKey
impl Signer<Signature> for PrivateKey
sourceimpl TryFrom<KeypairData> for PrivateKey
impl TryFrom<KeypairData> for PrivateKey
sourcefn try_from(key_data: KeypairData) -> Result<PrivateKey>
fn try_from(key_data: KeypairData) -> Result<PrivateKey>
Performs the conversion.
impl Eq for PrivateKey
subtle
only.Auto Trait Implementations
impl RefUnwindSafe for PrivateKey
impl Send for PrivateKey
impl Sync for PrivateKey
impl Unpin for PrivateKey
impl UnwindSafe for PrivateKey
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T, S> SignerMut<S> for T where
T: Signer<S>,
S: Signature,
impl<T, S> SignerMut<S> for T where
T: Signer<S>,
S: Signature,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more