openssl_dh_compute_key
Computes shared secret for public value of remote DH public key and local DH key
&reftitle.description;
stringfalseopenssl_dh_compute_key
stringpublic_key
OpenSSLAsymmetricKeyprivate_key
The shared secret returned by openssl_dh_compute_key is
often used as an encryption key to secretly communicate with a remote party.
This is known as the Diffie-Hellman key exchange.
&reftitle.parameters;
public_key
DH Public key of the remote party.
private_key
A local DH private key, corresponding to the public key to be shared with the remote party.
&reftitle.returnvalues;
Returns shared secret on success&return.falseforfailure;.
&reftitle.changelog;
&Version;
&Description;
8.0.0
private_key accepts an OpenSSLAsymmetricKey now;
previously, a &resource; of type OpenSSL key was accepted.
&reftitle.examples;
Compute a shared secret
First generate a public/private DH keypair locally, and have
the remote party do the same. We need to use the openssl
command-line utility.
Next, send your public key to the remote party. Use the openssl
pkey command to view the public key you will be sent from
the remote party.
&example.outputs.similar;
Use this public key as a parameter to openssl_dh_compute_key
in order to compute the shared secret.
]]>
Generate a DH public/private keypair in php
First, generate the DH prime number
&example.outputs.similar;
Prime and generator values ares passed as p and g into openssl_pkey_new
$configargs));
openssl_pkey_export_to_file($private_key,'privatekey.pem',$passphrase='y0urp@s5phr@se');
$details = openssl_pkey_get_details($private_key);
$local_pub_key = $details['dh']['pub_key'];
echo bin2hex($local_pub_key)."\n";//you can send your public key to the remote party
$details = openssl_pkey_get_details(openssl_pkey_get_public("file://remotepublickey.pem"));
$remote_public_key = $details['dh']['pub_key'];
$shared_secret = openssl_dh_compute_key($remote_public_key, $private_key);
echo bin2hex($shared_secret)."\n";
?>
]]>
&reftitle.seealso;
openssl_pkey_new
openssl_pkey_get_details
openssl_pkey_get_private
openssl_pkey_get_public