Example #1
0
 /**
  * Compute the shared secret key based on the public key received from the
  * the second party to this transaction. This should agree to the secret
  * key the second party computes on our own public key.
  * Once in agreement, the key is known to only to both parties.
  * By default, the function expects the public key to be in binary form
  * which is the typical format when being transmitted.
  *
  * If you need the binary form of the shared secret key, call
  * getSharedSecretKey() with the optional parameter for Binary output.
  *
  * @param string $publicKey
  * @param string $type
  * @param string $output
  * @throws Zend_Crypt_DiffieHellman_Exception
  * @return mixed
  */
 public function computeSecretKey($publicKey, $type = self::NUMBER, $output = self::NUMBER)
 {
     if ($type == self::BINARY) {
         $publicKey = $this->_math->fromBinary($publicKey);
     }
     if (!preg_match("/^\\d+\$/", $publicKey)) {
         throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
     }
     if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
         $this->_secretKey = openssl_dh_compute_key($publicKey, $this->getPublicKey());
     } else {
         $this->_secretKey = $this->_math->powmod($publicKey, $this->getPrivateKey(), $this->getPrime());
     }
     return $this->getSharedSecretKey($output);
 }