Content · Web Development · PHP

PHP openssl_dh_compute_key() Function: Usage and Examples

openssl_dh_compute_key computes a Diffie–Hellman shared key from local private-key parameters and the peer public.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

openssl_dh_compute_key computes a Diffie–Hellman shared key from local private-key parameters and the peer public value.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: openssl_dh_compute_key computes a Diffie–Hellman shared key from local private-key parameters and the peer public value.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_dh_compute_key(string $public_key, OpenSSLAsymmetricKey $private_key): string|false
ParameterPurpose
$public_key (string)A public key that belongs to the intended, verified peer.
$private_key (OpenSSLAsymmetricKey)The corresponding private key; never expose or log its PEM data.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: Key agreement requires validated peer keys and usually a KDF; raw shared-secret bytes are not ready-made application encryption keys.

Example 1: Basic Operation


This uses a deliberately small test group to keep the example fast. Production requires approved groups, peer authentication and key derivation.

<?php
$alice = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'private_key_bits' => 512]);
$parameters = openssl_pkey_get_details($alice)['dh'];
$bob = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'dh' => ['p' => $parameters['p'], 'g' => $parameters['g']]]);
$alicePublic = $parameters['pub_key'];
$bobPublic = openssl_pkey_get_details($bob)['dh']['pub_key'];
$aliceShared = openssl_dh_compute_key($bobPublic, $alice);
$bobShared = openssl_dh_compute_key($alicePublic, $bob);
var_dump(hash_equals($aliceShared, $bobShared));
// This small group demonstrates only matching parameters, not production security.

Example 2: A Different Use Case


Check the agreed DH secret from both participants without printing secret data.

<?php
// Check the alternative case.
$alice = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'private_key_bits' => 512]);
$parameters = openssl_pkey_get_details($alice)['dh'];
$bob = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'dh' => ['p' => $parameters['p'], 'g' => $parameters['g']]]);
$alicePublic = $parameters['pub_key'];
$bobPublic = openssl_pkey_get_details($bob)['dh']['pub_key'];
$aliceShared = openssl_dh_compute_key($bobPublic, $alice);
$bobShared = openssl_dh_compute_key($alicePublic, $bob);
echo strlen($aliceShared) . " derived bytes";
// This small group demonstrates only matching parameters, not production security.

Practical Example: Checking the Result


Check the agreed DH secret from both participants without printing secret data.

<?php
// Verify the expected behavior before using results.
$alice = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'private_key_bits' => 512]);
$parameters = openssl_pkey_get_details($alice)['dh'];
$bob = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_DH, 'dh' => ['p' => $parameters['p'], 'g' => $parameters['g']]]);
$alicePublic = $parameters['pub_key'];
$bobPublic = openssl_pkey_get_details($bob)['dh']['pub_key'];
$aliceShared = openssl_dh_compute_key($bobPublic, $alice);
$bobShared = openssl_dh_compute_key($alicePublic, $bob);
var_dump(is_string($bobShared) && strlen($bobShared) === strlen($aliceShared));
// This small group demonstrates only matching parameters, not production security.

Important Behavior and Edge Cases


Key agreement requires validated peer keys and usually a KDF; raw shared-secret bytes are not ready-made application encryption keys.

Key agreement requires validated peer keys and usually a KDF; raw shared-secret bytes are not ready-made application encryption keys.

Comparison with Related Functions


ApproachKey distinction
openssl_dh_compute_keyopenssl_dh_compute_key computes a Diffie–Hellman shared key from local private-key parameters and the peer public value.
Related techniqueopenssl_pkey_derive supports key derivation from modern asymmetric key objects.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismopenssl_pkey_derive supports key derivation from modern asymmetric key objects.
Ignoring a security prerequisite or failure caseKey agreement requires validated peer keys and usually a KDF; raw shared-secret bytes are not ready-made application encryption keys.

Key Takeaways


  • openssl_dh_compute_key computes a Diffie–Hellman shared key from local private-key parameters and the peer public value.
  • Key agreement requires validated peer keys and usually a KDF; raw shared-secret bytes are not ready-made application encryption keys.
  • openssl_pkey_derive supports key derivation from modern asymmetric key objects.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.