Computes shared secret for public value of remote and local DH or ECDH key

- « openssl_pkcs7_verify

- openssl_pkey_export_to_file »

- PHP Manual
- OpenSSL Functions
- Computes shared secret for public value of remote and local DH or ECDH key

# openssl_pkey_derive

(PHP 7 >= 7.3.0, PHP 8)

openssl_pkey_derive — Computes shared secret for public value of remote and local DH or ECDH key

### Description

openssl_pkey_derive(OpenSSLAsymmetricKey|OpenSSLCertificate|array|string `$public_key`, OpenSSLAsymmetricKey|OpenSSLCertificate|array|string `$private_key`, int `$key_length` = 0): string|false

openssl_pkey_derive() takes a set of a `public_key` and `private_key` and derives a shared secret, for either DH or EC keys.

### Parameters

`public_key`

  `public_key` is the public key for the derivation. See Public/Private Key parameters for a list of valid values.

`private_key`

  `private_key` is the private key for the derivation. See Public/Private Key parameters for a list of valid values.

`key_length`

  If not zero, will set the desired length of the derived secret.

### Return Values

The derived secret on success or `false` on failure.

### Examples

Example #1 openssl_pkey_derive() example

    <?php// Load in private key$priv = openssl_pkey_get_private("-----BEGIN PRIVATE   PRIVATE KEY-----");// Load in public key$pub = openssl_pkey_get_public("-----BEGIN PUBLIC   PUBLIC KEY-----");// Outputs the hex version of the derived keyecho bin2hex(openssl_pkey_derive($pub,$priv));