# Struct openssl::pkey::PKey [−] [src]

    pub struct PKey<T>(_, _);

A public or private key.

## Implementations

### `impl<T> PKey<T>` [src]

#### `pub fn from_rsa(rsa: Rsa<T>) -> Result<PKey<T>, ErrorStack>` [src]

Creates a new `PKey` containing an RSA key.

This corresponds to `EVP_PKEY_assign_RSA`.

#### `pub fn from_dsa(dsa: Dsa<T>) -> Result<PKey<T>, ErrorStack>` [src]

Creates a new `PKey` containing a DSA key.

This corresponds to `EVP_PKEY_assign_DSA`.

#### `pub fn from_dh(dh: Dh<T>) -> Result<PKey<T>, ErrorStack>` [src]

Creates a new `PKey` containing a Diffie-Hellman key.

This corresponds to `EVP_PKEY_assign_DH`.

#### `pub fn from_ec_key(ec_key: EcKey<T>) -> Result<PKey<T>, ErrorStack>` [src]

Creates a new `PKey` containing an elliptic curve key.

This corresponds to `EVP_PKEY_assign_EC_KEY`.

### `impl PKey<Private>` [src]

#### `pub fn hmac(key: &[u8]) -> Result<PKey<Private>, ErrorStack>` [src]

Creates a new `PKey` containing an HMAC key.

# Note

To compute HMAC values, use the `sign` module.

#### `pub fn cmac(cipher: &Cipher, key: &[u8]) -> Result<PKey<Private>, ErrorStack>` [src]

Creates a new `PKey` containing a CMAC key.

Requires OpenSSL 1.1.0 or newer.

# Note

To compute CMAC values, use the `sign` module.

#### `pub fn generate_x25519() -> Result<PKey<Private>, ErrorStack>` [src]

Generates a new private Ed25519 key

#### `pub fn generate_x448() -> Result<PKey<Private>, ErrorStack>` [src]

Generates a new private Ed448 key

#### `pub fn generate_ed25519() -> Result<PKey<Private>, ErrorStack>` [src]

Generates a new private Ed25519 key

#### `pub fn generate_ed448() -> Result<PKey<Private>, ErrorStack>` [src]

Generates a new private Ed448 key

#### `pub fn private_key_from_pem(pem: &[u8]) -> Result<PKey<Private>, ErrorStack>` [src]

Deserializes a private key from a PEM-encoded key type specific format.

This corresponds to `PEM_read_bio_PrivateKey`.

#### `pub fn private_key_from_pem_passphrase(
pem: &[u8],
passphrase: &[u8]
) -> Result<PKey<Private>, ErrorStack>` [src]

Deserializes a private key from a PEM-encoded encrypted key type specific format.

This corresponds to `PEM_read_bio_PrivateKey`.

#### `pub fn private_key_from_pem_callback<F>(
pem: &[u8],
callback: F
) -> Result<PKey<Private>, ErrorStack> where
F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>,` [src]

Deserializes a private key from a PEM-encoded encrypted key type specific format.

The callback should fill the password into the provided buffer and return its length.

This corresponds to `PEM_read_bio_PrivateKey`.

#### `pub fn private_key_from_der(der: &[u8]) -> Result<PKey<Private>, ErrorStack>` [src]

Decodes a DER-encoded private key.

This function will automatically attempt to detect the underlying key format, and supports the unencrypted PKCS#8 PrivateKeyInfo structures as well as key type specific formats.

This corresponds to `d2i_AutoPrivateKey`.

#### `pub fn private_key_from_pkcs8(der: &[u8]) -> Result<PKey<Private>, ErrorStack>` [src]

Deserializes a DER-formatted PKCS#8 unencrypted private key.

This method is mainly for interoperability reasons. Encrypted keyfiles should be preferred.

#### `pub fn private_key_from_pkcs8_callback<F>(
der: &[u8],
callback: F
) -> Result<PKey<Private>, ErrorStack> where
F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>,` [src]

Deserializes a DER-formatted PKCS#8 private key, using a callback to retrieve the password if the key is encrpyted.

The callback should copy the password into the provided buffer and return the number of bytes written.

#### `pub fn private_key_from_pkcs8_passphrase(
der: &[u8],
passphrase: &[u8]
) -> Result<PKey<Private>, ErrorStack>` [src]

Deserializes a DER-formatted PKCS#8 private key, using the supplied password if the key is encrypted.

# Panics

Panics if `passphrase` contains an embedded null.

### `impl PKey<Public>` [src]

#### `pub fn public_key_from_pem(pem: &[u8]) -> Result<PKey<Public>, ErrorStack>` [src]

Decodes a PEM-encoded SubjectPublicKeyInfo structure.

The input should have a header of `-----BEGIN PUBLIC KEY-----`.

This corresponds to `PEM_read_bio_PUBKEY`.

#### `pub fn public_key_from_der(der: &[u8]) -> Result<PKey<Public>, ErrorStack>` [src]

Decodes a DER-encoded SubjectPublicKeyInfo structure.

This corresponds to `d2i_PUBKEY`.

## Methods from Deref<Target = PKeyRef<T>>

#### `pub fn rsa(&self) -> Result<Rsa<T>, ErrorStack>` [src]

Returns a copy of the internal RSA key.

This corresponds to `EVP_PKEY_get1_RSA`.

#### `pub fn dsa(&self) -> Result<Dsa<T>, ErrorStack>` [src]

Returns a copy of the internal DSA key.

This corresponds to `EVP_PKEY_get1_DSA`.

#### `pub fn dh(&self) -> Result<Dh<T>, ErrorStack>` [src]

Returns a copy of the internal DH key.

This corresponds to `EVP_PKEY_get1_DH`.

#### `pub fn ec_key(&self) -> Result<EcKey<T>, ErrorStack>` [src]

Returns a copy of the internal elliptic curve key.

This corresponds to `EVP_PKEY_get1_EC_KEY`.

#### `pub fn id(&self) -> Id` [src]

Returns the `Id` that represents the type of this key.

This corresponds to `EVP_PKEY_id`.

#### `pub fn size(&self) -> usize` [src]

Returns the maximum size of a signature in bytes.

This corresponds to `EVP_PKEY_size`.

#### `pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack>` [src]

Serializes the public key into a PEM-encoded SubjectPublicKeyInfo structure.

The output will have a header of `-----BEGIN PUBLIC KEY-----`.

This corresponds to `PEM_write_bio_PUBKEY`.

#### `pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack>` [src]

Serializes the public key into a DER-encoded SubjectPublicKeyInfo structure.

This corresponds to `i2d_PUBKEY`.

#### `pub fn bits(&self) -> u32` [src]

Returns the size of the key.

This corresponds to the bit length of the modulus of an RSA key, and the bit length of the group order for an elliptic curve key, for example.

#### `pub fn public_eq<U>(&self, other: &PKeyRef<U>) -> bool where
U: HasPublic,` [src]

Compares the public component of this key with another.

#### `pub fn private_key_to_pem_pkcs8(&self) -> Result<Vec<u8>, ErrorStack>` [src]

Serializes the private key to a PEM-encoded PKCS#8 PrivateKeyInfo structure.

The output will have a header of `-----BEGIN PRIVATE KEY-----`.

This corresponds to `PEM_write_bio_PKCS8PrivateKey`.

#### `pub fn private_key_to_pem_pkcs8_passphrase(
&self,
cipher: Cipher,
passphrase: &[u8]
) -> Result<Vec<u8>, ErrorStack>` [src]

Serializes the private key to a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo structure.

The output will have a header of `-----BEGIN ENCRYPTED PRIVATE KEY-----`.

This corresponds to `PEM_write_bio_PKCS8PrivateKey`.

#### `pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack>` [src]

Serializes the private key to a DER-encoded key type specific format.

This corresponds to `i2d_PrivateKey`.

## Trait Implementations

### `impl<T> AsRef<PKeyRef<T>> for PKey<T>` [src]

#### `fn as_ref(&self) -> &PKeyRef<T>` [src]

Performs the conversion.

### `impl<T> Borrow<PKeyRef<T>> for PKey<T>` [src]

#### `fn borrow(&self) -> &PKeyRef<T>` [src]

Immutably borrows from an owned value. Read more

### `impl<T> Clone for PKey<T>` [src]

#### `fn clone(&self) -> PKey<T>` [src]

Returns a copy of the value. Read more

#### `pub fn clone_from(&mut self, source: &Self)`1.0.0 [src]

Performs copy-assignment from `source`. Read more

### `impl<T> Debug for PKey<T>` [src]

#### `fn fmt(&self, fmt: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl<T> Deref for PKey<T>` [src]

#### `type Target = PKeyRef<T>`

The resulting type after dereferencing.

#### `fn deref(&self) -> &PKeyRef<T>` [src]

Dereferences the value.

### `impl<T> DerefMut for PKey<T>` [src]

#### `fn deref_mut(&mut self) -> &mut PKeyRef<T>` [src]

Mutably dereferences the value.

### `impl<T> Drop for PKey<T>` [src]

#### `fn drop(&mut self)` [src]

Executes the destructor for this type. Read more

### `impl<T> ForeignType for PKey<T>` [src]

#### `type CType = EVP_PKEY`

The raw C type.

#### `type Ref = PKeyRef<T>`

The type representing a reference to this type.

#### `unsafe fn from_ptr(ptr: *mut EVP_PKEY) -> PKey<T>` [src]

Constructs an instance of this type from its raw type.

#### `fn as_ptr(&self) -> *mut EVP_PKEY` [src]

Returns a raw pointer to the wrapped value.

### `impl<T> Send for PKey<T>` [src]

### `impl<T> Sync for PKey<T>` [src]

## Auto Trait Implementations

### `impl<T> RefUnwindSafe for PKey<T> where
T: RefUnwindSafe,`

### `impl<T> Unpin for PKey<T> where
T: Unpin,`

### `impl<T> UnwindSafe for PKey<T> where
T: UnwindSafe,`

## Blanket Implementations

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T` [src]

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `pub fn to_owned(&self) -> T` [src]

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

#### `pub fn clone_into(&self, target: &mut T)` [src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into`)

recently added

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

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

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

The type returned in the event of a conversion error.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.