Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Static Public Attributes | Protected Member Functions
Zend_Crypt_DiffieHellman Class Reference

Public Member Functions

 __construct ($prime, $generator, $privateKey=null, $privateKeyType=self::NUMBER)
 
 generateKeys ()
 
 setPublicKey ($number, $type=self::NUMBER)
 
 getPublicKey ($type=self::NUMBER)
 
 computeSecretKey ($publicKey, $type=self::NUMBER, $output=self::NUMBER)
 
 getSharedSecretKey ($type=self::NUMBER)
 
 setPrime ($number)
 
 getPrime ()
 
 setGenerator ($number)
 
 getGenerator ()
 
 setPrivateKey ($number, $type=self::NUMBER)
 
 getPrivateKey ($type=self::NUMBER)
 
 hasPrivateKey ()
 
 setBigIntegerMath ($extension=null)
 

Data Fields

const BINARY = 'binary'
 
const NUMBER = 'number'
 
const BTWOC = 'btwoc'
 

Static Public Attributes

static $useOpenssl = true
 

Protected Member Functions

 _generatePrivateKey ()
 

Detailed Description

Definition at line 33 of file DiffieHellman.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $prime,
  $generator,
  $privateKey = null,
  $privateKeyType = self::NUMBER 
)

Constructor; if set construct the object using the parameter array to set values for Prime, Generator and Private. If a Private Key is not set, one will be generated at random.

Parameters
string$prime
string$generator
string$privateKey
string$privateKeyType

Definition at line 106 of file DiffieHellman.php.

107  {
108  $this->setPrime($prime);
109  $this->setGenerator($generator);
110  if ($privateKey !== null) {
111  $this->setPrivateKey($privateKey, $privateKeyType);
112  }
113  $this->setBigIntegerMath();
114  }
setBigIntegerMath($extension=null)
setPrivateKey($number, $type=self::NUMBER)

Member Function Documentation

◆ _generatePrivateKey()

_generatePrivateKey ( )
protected

In the event a private number/key has not been set by the user, or generated by ext/openssl, a best attempt will be made to generate a random key. Having a random number generator installed on linux/bsd is highly recommended! The alternative is not recommended for production unless without any other option.

Returns
string

Definition at line 382 of file DiffieHellman.php.

383  {
384  $rand = $this->_math->rand($this->getGenerator(), $this->getPrime());
385  return $rand;
386  }

◆ computeSecretKey()

computeSecretKey (   $publicKey,
  $type = self::NUMBER,
  $output = self::NUMBER 
)

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.

Parameters
string$publicKey
string$type
string$output
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
mixed

Definition at line 203 of file DiffieHellman.php.

204  {
205  if ($type == self::BINARY) {
206  $publicKey = $this->_math->fromBinary($publicKey);
207  }
208  if (!preg_match("/^\d+$/", $publicKey)) {
209  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
210  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
211  }
212  if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
213  $this->_secretKey = openssl_dh_compute_key($publicKey, $this->getPublicKey());
214  } else {
215  $this->_secretKey = $this->_math->powmod($publicKey, $this->getPrivateKey(), $this->getPrime());
216  }
217  return $this->getSharedSecretKey($output);
218  }
getPrivateKey($type=self::NUMBER)
getPublicKey($type=self::NUMBER)
$type
Definition: item.phtml:13
getSharedSecretKey($type=self::NUMBER)

◆ generateKeys()

generateKeys ( )

Generate own public key. If a private number has not already been set, one will be generated at this stage.

Returns
Zend_Crypt_DiffieHellman

Definition at line 122 of file DiffieHellman.php.

123  {
124  if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
125  $details = array();
126  $details['p'] = $this->getPrime();
127  $details['g'] = $this->getGenerator();
128  if ($this->hasPrivateKey()) {
129  $details['priv_key'] = $this->getPrivateKey();
130  }
131  $opensslKeyResource = openssl_pkey_new( array('dh' => $details) );
132  $data = openssl_pkey_get_details($opensslKeyResource);
133  $this->setPrivateKey($data['dh']['priv_key'], self::BINARY);
134  $this->setPublicKey($data['dh']['pub_key'], self::BINARY);
135  } else {
136  // Private key is lazy generated in the absence of PHP 5.3's ext/openssl
137  $publicKey = $this->_math->powmod($this->getGenerator(), $this->getPrivateKey(), $this->getPrime());
138  $this->setPublicKey($publicKey);
139  }
140  return $this;
141  }
getPrivateKey($type=self::NUMBER)
$details
Definition: vault.phtml:10
setPrivateKey($number, $type=self::NUMBER)
setPublicKey($number, $type=self::NUMBER)

◆ getGenerator()

getGenerator ( )

Getter for the value of the generator number

Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
string

Definition at line 296 of file DiffieHellman.php.

297  {
298  if (!isset($this->_generator)) {
299  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
300  throw new Zend_Crypt_DiffieHellman_Exception('No generator number has been set');
301  }
302  return $this->_generator;
303  }

◆ getPrime()

getPrime ( )

Getter for the value of the prime number

Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
string

Definition at line 264 of file DiffieHellman.php.

265  {
266  if (!isset($this->_prime)) {
267  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
268  throw new Zend_Crypt_DiffieHellman_Exception('No prime number has been set');
269  }
270  return $this->_prime;
271  }

◆ getPrivateKey()

getPrivateKey (   $type = self::NUMBER)

Getter for the value of the private number

Parameters
string$type
Returns
string

Definition at line 332 of file DiffieHellman.php.

333  {
334  if (!$this->hasPrivateKey()) {
335  $this->setPrivateKey($this->_generatePrivateKey(), self::BINARY);
336  }
337  if ($type == self::BINARY) {
338  return $this->_math->toBinary($this->_privateKey);
339  } elseif ($type == self::BTWOC) {
340  return $this->_math->btwoc($this->_math->toBinary($this->_privateKey));
341  }
342  return $this->_privateKey;
343  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
setPrivateKey($number, $type=self::NUMBER)

◆ getPublicKey()

getPublicKey (   $type = self::NUMBER)

Returns own public key for communication to the second party to this transaction.

Parameters
string$type
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
string

Definition at line 172 of file DiffieHellman.php.

173  {
174  if ($this->_publicKey === null) {
175  #require_once 'Zend/Crypt/DiffieHellman/Exception.php';
176  throw new Zend_Crypt_DiffieHellman_Exception('A public key has not yet been generated using a prior call to generateKeys()');
177  }
178  if ($type == self::BINARY) {
179  return $this->_math->toBinary($this->_publicKey);
180  } elseif ($type == self::BTWOC) {
181  return $this->_math->btwoc($this->_math->toBinary($this->_publicKey));
182  }
183  return $this->_publicKey;
184  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13

◆ getSharedSecretKey()

getSharedSecretKey (   $type = self::NUMBER)

Return the computed shared secret key from the DiffieHellman transaction

Parameters
string$type
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
string

Definition at line 227 of file DiffieHellman.php.

228  {
229  if (!isset($this->_secretKey)) {
230  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
231  throw new Zend_Crypt_DiffieHellman_Exception('A secret key has not yet been computed; call computeSecretKey()');
232  }
233  if ($type == self::BINARY) {
234  return $this->_math->toBinary($this->_secretKey);
235  } elseif ($type == self::BTWOC) {
236  return $this->_math->btwoc($this->_math->toBinary($this->_secretKey));
237  }
238  return $this->_secretKey;
239  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13

◆ hasPrivateKey()

hasPrivateKey ( )

Check whether a private key currently exists.

Returns
boolean

Definition at line 350 of file DiffieHellman.php.

351  {
352  return isset($this->_privateKey);
353  }

◆ setBigIntegerMath()

setBigIntegerMath (   $extension = null)

Setter to pass an extension parameter which is used to create a specific BigInteger instance for a specific extension type. Allows manual setting of the class in case of an extension problem or bug.

Parameters
string$extension
Returns
void
See also
Zend_Crypt_Math

Definition at line 364 of file DiffieHellman.php.

365  {
369  #require_once 'Zend/Crypt/Math.php';
370  $this->_math = new Zend_Crypt_Math($extension);
371  }

◆ setGenerator()

setGenerator (   $number)

Setter for the value of the generator number

Parameters
string$number
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
Zend_Crypt_DiffieHellman

Definition at line 280 of file DiffieHellman.php.

281  {
282  if (!preg_match("/^\d+$/", $number) || $number < 2) {
283  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
284  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number greater than 1');
285  }
286  $this->_generator = (string) $number;
287  return $this;
288  }
$number
Definition: details.phtml:22

◆ setPrime()

setPrime (   $number)

Setter for the value of the prime number

Parameters
string$number
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
Zend_Crypt_DiffieHellman

Definition at line 248 of file DiffieHellman.php.

249  {
250  if (!preg_match("/^\d+$/", $number) || $number < 11) {
251  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
252  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number or too small: should be a large natural number prime');
253  }
254  $this->_prime = (string) $number;
255  return $this;
256  }
$number
Definition: details.phtml:22

◆ setPrivateKey()

setPrivateKey (   $number,
  $type = self::NUMBER 
)

Setter for the value of the private number

Parameters
string$number
string$type
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
Zend_Crypt_DiffieHellman

Definition at line 313 of file DiffieHellman.php.

314  {
315  if ($type == self::BINARY) {
316  $number = $this->_math->fromBinary($number);
317  }
318  if (!preg_match("/^\d+$/", $number)) {
319  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
320  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
321  }
322  $this->_privateKey = (string) $number;
323  return $this;
324  }
$number
Definition: details.phtml:22
$type
Definition: item.phtml:13

◆ setPublicKey()

setPublicKey (   $number,
  $type = self::NUMBER 
)

Setter for the value of the public number

Parameters
string$number
string$type
Exceptions
Zend_Crypt_DiffieHellman_Exception
Returns
Zend_Crypt_DiffieHellman

Definition at line 151 of file DiffieHellman.php.

152  {
153  if ($type == self::BINARY) {
154  $number = $this->_math->fromBinary($number);
155  }
156  if (!preg_match("/^\d+$/", $number)) {
157  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
158  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
159  }
160  $this->_publicKey = (string) $number;
161  return $this;
162  }
$number
Definition: details.phtml:22
$type
Definition: item.phtml:13

Field Documentation

◆ $useOpenssl

$useOpenssl = true
static

Definition at line 42 of file DiffieHellman.php.

◆ BINARY

const BINARY = 'binary'

Constants

Definition at line 92 of file DiffieHellman.php.

◆ BTWOC

const BTWOC = 'btwoc'

Definition at line 94 of file DiffieHellman.php.

◆ NUMBER

const NUMBER = 'number'

Definition at line 93 of file DiffieHellman.php.


The documentation for this class was generated from the following file: