note 125624 added to function.openssl-dh-compute-key

From: Date: Tue, 22 Dec 2020 08:08:17 +0000
Subject: note 125624 added to function.openssl-dh-compute-key
Groups: php.notes 
Request: Send a blank email to php-notes+get-216016@lists.php.net to get a copy of this message
A working example. After some study and reading I finally get how this method is working. You need to follow the below 4 steps; 1. You create a public key which is known to 1:n parties. 2. Each party creates their own keypair. 2a. Each party shared their public key with the members. 3. Each user can re-create the shared secret by using his Private Key and the Public Key of the other parties. 4. Compare the secrets as a handshake /* 1. Create the first, global known public key. */ /** * Get DH public/private keys * @return array */ public static function get_keypair() { $keys = []; $config = [ "digest_alg" => "sha512", "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_DH, ]; // Create the private and public key $res = openssl_pkey_new($config); $pubKey = openssl_pkey_get_details($res); $keys["public"] = $pubKey["key"]; openssl_pkey_export($res, $privKey); $keys["private"] = $privKey; return $keys; } Now you share the Public Key with every member of the party. /* 2. Each user creates a new Key Pair with the P,G from the global public key info */ $key = openssl_get_publickey(base64_decode($publicKey)); $info = openssl_pkey_get_details($key); $params = $info["dh"]; Now you have the P,G from the public key. Use it; /** * Create keypair from Prime and Generator for KeyExchange * @param $prime * @param $generator */ public static function create_keypair_from_pg($prime, $generator) { $config = [ "digest_alg" => "sha512", "private_key_bits" => 2048, "dh" => [ "p" => $prime, "g" => $generator ], "private_key_type" => OPENSSL_KEYTYPE_DH, ]; return openssl_pkey_new($config); } /* 3. Create a shared secret with your Private Key, and User 1:n's Public Key */ $privateKey = openssl_get_publickey(base64_decode($privateKeyData)); $secret1 = openssl_dh_compute_key($user1PublicKey, $privateKey); if($secret !== false) { return bin2hex($secret); }else{ print_r(openssl_error_string()); } $secret2 = openssl_dh_compute_key($user2PublicKey, $privateKey); if($secret !== false) { return bin2hex($secret); }else{ print_r(openssl_error_string()); } /* 4. Compare the secrets as a handshake method */ if(strcmp($secret1, $secret2) === 0) { return true; } return false; Good luck, enjoy!. Keep me posted about improvements and updates. vangelier AT hotmail DOT com ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.165 (proxied: 78.78.155.161) ---- Manual Page -- http://php.net/manual/en/function.openssl-dh-compute-key.php Edit -- https://master.php.net/note/edit/125624 Del: integrated -- https://master.php.net/note/delete/125624/integrated Del: useless -- https://master.php.net/note/delete/125624/useless Del: bad code -- https://master.php.net/note/delete/125624/bad+code Del: spam -- https://master.php.net/note/delete/125624/spam Del: non-english -- https://master.php.net/note/delete/125624/non-english Del: in docs -- https://master.php.net/note/delete/125624/in+docs Del: other reasons-- https://master.php.net/note/delete/125624 Reject -- https://master.php.net/note/reject/125624 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#216016) next »