Struct osshkeys::keys::KeyPair

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

General key pair type

This is a type to make it easy to store different types of key pair in the container. Each can contain one of the types supported in this crate.

Key pair is the so-called “private key” which contains both public and private parts of an asymmetry key.

Implementations§

source§

impl KeyPair

source

pub fn from_keystr(pem: &str, passphrase: Option<&str>) -> OsshResult<Self>

Parse a keypair from supporting file types

The passphrase is required if the keypair is encrypted.

OpenSSL PEM
  • Begin with -----BEGIN DSA PRIVATE KEY----- for dsa key.
  • Begin with -----BEGIN RSA PRIVATE KEY----- for rsa key.
  • Begin with -----BEGIN EC PRIVATE KEY----- for ecdsa key.
  • Begin with -----BEGIN PRIVATE KEY----- for Ed25519 key.
PKCS#8 Format
  • Begin with -----BEGIN PRIVATE KEY-----
Openssh
  • Begin with -----BEGIN OPENSSH PRIVATE KEY-----

This is the new format which is supported since OpenSSH 6.5, and it became the default format in OpenSSH 7.8. The Ed25519 key can only be stored in this type.

source

pub fn generate(keytype: KeyType, bits: usize) -> OsshResult<Self>

Generate a key of the specified type and size

Key Size

There are some limitations to the key size:

  • RSA: the size should >= 1024 and <= 16384 bits.
  • DSA: the size should be 1024 bits.
  • EcDSA: the size should be 256, 384, or 521 bits.
  • Ed25519: the size should be 256 bits.

If the key size parameter is zero, then it will use the default size to generate the key

  • RSA: 2048 bits
  • DSA: 1024 bits
  • EcDSA: 256 bits
  • Ed25519: 256 bits
source

pub fn keytype(&self) -> KeyType

Indicate the key type being stored

source

pub fn serialize_pem(&self, passphrase: Option<&str>) -> OsshResult<String>

Serialize the keypair to the OpenSSL PEM format

If the passphrase is given (set to Some(...)), then the generated PEM key will be encrypted.

source

pub fn serialize_pkcs8(&self, passphrase: Option<&str>) -> OsshResult<String>

Serialize the keypair to the OpenSSL PKCS#8 PEM format

If the passphrase is given (set to Some(...)), then the generated PKCS#8 key will be encrypted.

source

pub fn serialize_openssh( &self, passphrase: Option<&str>, cipher: Cipher ) -> OsshResult<String>

Serialize the keypair to the OpenSSH private key format

If the passphrase is given (set to Some(...)) and cipher is not null, then the generated private key will be encrypted.

source

pub fn comment(&self) -> &str

Get the comment of the key

source

pub fn comment_mut(&mut self) -> &mut String

Get the mutable reference of the key comment

source

pub fn serialize_publickey(&self) -> OsshResult<String>

Get the OpenSSH public key of the public parts

source

pub fn clone_public_key(&self) -> Result<PublicKey, Error>

Clone the public parts of the key pair

Trait Implementations§

source§

impl From<DsaKeyPair> for KeyPair

source§

fn from(inner: DsaKeyPair) -> KeyPair

Converts to this type from the input type.
source§

impl From<EcDsaKeyPair> for KeyPair

source§

fn from(inner: EcDsaKeyPair) -> KeyPair

Converts to this type from the input type.
source§

impl From<Ed25519KeyPair> for KeyPair

source§

fn from(inner: Ed25519KeyPair) -> KeyPair

Converts to this type from the input type.
source§

impl From<RsaKeyPair> for KeyPair

source§

fn from(inner: RsaKeyPair) -> KeyPair

Converts to this type from the input type.
source§

impl Key for KeyPair

source§

fn size(&self) -> usize

The size in bits of the key
source§

fn keyname(&self) -> &'static str

The key name of the key
source§

fn short_keyname(&self) -> &'static str

The short key name of the key
source§

impl PrivateParts for KeyPair

source§

fn sign(&self, data: &[u8]) -> Result<Vec<u8>, Error>

Sign the data with the key, returning the “detached” signature
source§

impl PublicParts for KeyPair

source§

fn verify(&self, data: &[u8], sig: &[u8]) -> Result<bool, Error>

Verify the data with a detached signature, returning true if the signature is not malformed
source§

fn blob(&self) -> Result<Vec<u8>, Error>

Return the binary representation of the public key
source§

fn fingerprint(&self, hash: FingerprintHash) -> OsshResult<Vec<u8>>

Hash the blob of the public key to generate the fingerprint
source§

fn fingerprint_randomart(&self, hash: FingerprintHash) -> OsshResult<String>

Draw an ASCII-art picture from the fingerprint, also known as “randomart”

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V