Struct ssh_key::private::PrivateKey

source ·
pub struct PrivateKey { /* private fields */ }
Expand description

SSH private key.

Implementations§

source§

impl PrivateKey

source

pub fn new(key_data: KeypairData, comment: impl Into<String>) -> Result<Self>

Create a new unencrypted private key with the given keypair data and comment.

On no_std platforms, use PrivateKey::from(key_data) instead.

source

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-----
source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Parse a raw binary SSH private key.

source

pub fn encode_openssh<'o>( &self, line_ending: LineEnding, out: &'o mut [u8] ) -> Result<&'o str>

Encode OpenSSH-formatted (PEM) private key.

source

pub fn to_openssh(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>

Encode an OpenSSH-formatted PEM private key, allocating a self-zeroizing String for the result.

source

pub fn to_bytes(&self) -> Result<Zeroizing<Vec<u8>>>

Serialize SSH private key as raw bytes.

source

pub fn read_openssh_file(path: &Path) -> Result<Self>

Read private key from an OpenSSH-formatted PEM file.

source

pub fn write_openssh_file( &self, path: &Path, line_ending: LineEnding ) -> Result<()>

Write private key as an OpenSSH-formatted PEM file.

source

pub fn algorithm(&self) -> Algorithm

Get the digital signature Algorithm used by this key.

source

pub fn comment(&self) -> &str

Comment on the key (e.g. email address).

source

pub fn cipher(&self) -> Cipher

Cipher algorithm (a.k.a. ciphername).

source

pub fn fingerprint(&self, hash_alg: HashAlg) -> Fingerprint

Compute key fingerprint.

Use Default::default() to use the default hash function (SHA-256).

source

pub fn is_encrypted(&self) -> bool

Is this key encrypted?

source

pub fn kdf(&self) -> &Kdf

Key Derivation Function (KDF) used to encrypt this key.

Returns Kdf::None if this key is not encrypted.

source

pub fn key_data(&self) -> &KeypairData

Keypair data.

source

pub fn public_key(&self) -> &PublicKey

Get the PublicKey which corresponds to this private key.

source

pub fn random( rng: impl CryptoRng + RngCore, algorithm: Algorithm ) -> Result<Self>

Generate a random key which uses the given algorithm.

§Returns
  • Error::Algorithm if the algorithm is unsupported.
source

pub fn set_comment(&mut self, comment: impl Into<String>)

Set the comment on the key.

Trait Implementations§

source§

impl Clone for PrivateKey

source§

fn clone(&self) -> PrivateKey

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PrivateKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&PrivateKey> for KeyData

source§

fn from(private_key: &PrivateKey) -> KeyData

Converts to this type from the input type.
source§

impl From<&PrivateKey> for PublicKey

source§

fn from(private_key: &PrivateKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<DsaKeypair> for PrivateKey

source§

fn from(keypair: DsaKeypair) -> PrivateKey

Converts to this type from the input type.
source§

impl From<EcdsaKeypair> for PrivateKey

source§

fn from(keypair: EcdsaKeypair) -> PrivateKey

Converts to this type from the input type.
source§

impl From<Ed25519Keypair> for PrivateKey

source§

fn from(keypair: Ed25519Keypair) -> PrivateKey

Converts to this type from the input type.
source§

impl From<PrivateKey> for KeyData

source§

fn from(private_key: PrivateKey) -> KeyData

Converts to this type from the input type.
source§

impl From<PrivateKey> for PublicKey

source§

fn from(private_key: PrivateKey) -> PublicKey

Converts to this type from the input type.
source§

impl From<RsaKeypair> for PrivateKey

source§

fn from(keypair: RsaKeypair) -> PrivateKey

Converts to this type from the input type.
source§

impl From<SkEcdsaSha2NistP256> for PrivateKey

source§

fn from(keypair: SkEcdsaSha2NistP256) -> PrivateKey

Converts to this type from the input type.
source§

impl From<SkEd25519> for PrivateKey

source§

fn from(keypair: SkEd25519) -> PrivateKey

Converts to this type from the input type.
source§

impl FromStr for PrivateKey

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl PemLabel for PrivateKey

source§

const PEM_LABEL: &'static str = "OPENSSH PRIVATE KEY"

Expected PEM type label for a given document, e.g. "PRIVATE KEY"
source§

fn validate_pem_label(actual: &str) -> Result<(), Error>

Validate that a given label matches the expected label.
source§

impl Signer<Signature> for PrivateKey

source§

fn try_sign(&self, message: &[u8]) -> Result<Signature>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
source§

fn sign(&self, msg: &[u8]) -> S

Sign the given message and return a digital signature
source§

impl TryFrom<KeypairData> for PrivateKey

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(key_data: KeypairData) -> Result<PrivateKey>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, S> SignerMut<S> for T
where T: Signer<S>, S: Signature,

source§

fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>

Attempt to sign the given message, updating the state, and returning a digital signature on success, or an error if something went wrong. Read more
source§

fn sign(&mut self, msg: &[u8]) -> S

Sign the given message, update the state, and return a digital signature
source§

impl<T> SigningKey for T
where T: Signer<Signature>, KeyData: for<'a> From<&'a T>,

source§

fn public_key(&self) -> KeyData

Get the public::KeyData for this signing key.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.