Zend Framework  3.0
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
DiffieHellman Class Reference

PHP implementation of the Diffie-Hellman public key encryption algorithm. More...

Public Member Functions

 __construct ($prime, $generator, $privateKey=null, $privateKeyFormat=self::FORMAT_NUMBER)
 Constructor; if set construct the object using the parameter array to set values for Prime, Generator and Private.
 
 generateKeys ()
 Generate own public key.
 
 setPublicKey ($number, $format=self::FORMAT_NUMBER)
 Setter for the value of the public number.
 
 getPublicKey ($format=self::FORMAT_NUMBER)
 Returns own public key for communication to the second party to this transaction.
 
 computeSecretKey ($publicKey, $publicKeyFormat=self::FORMAT_NUMBER, $secretKeyFormat=self::FORMAT_NUMBER)
 Compute the shared secret key based on the public key received from the the second party to this transaction.
 
 getSharedSecretKey ($format=self::FORMAT_NUMBER)
 Return the computed shared secret key from the DiffieHellman transaction.
 
 setPrime ($number)
 Setter for the value of the prime number.
 
 getPrime ($format=self::FORMAT_NUMBER)
 Getter for the value of the prime number.
 
 setGenerator ($number)
 Setter for the value of the generator number.
 
 getGenerator ($format=self::FORMAT_NUMBER)
 Getter for the value of the generator number.
 
 setPrivateKey ($number, $format=self::FORMAT_NUMBER)
 Setter for the value of the private number.
 
 getPrivateKey ($format=self::FORMAT_NUMBER)
 Getter for the value of the private number.
 
 hasPrivateKey ()
 Check whether a private key currently exists.
 

Static Public Member Functions

static useOpensslExtension ($flag=true)
 Set whether to use openssl extension.
 

Public Attributes

const DEFAULT_KEY_SIZE = 2048
 
const FORMAT_BINARY = 'binary'
 Key formats.
 
const FORMAT_NUMBER = 'number'
 
const FORMAT_BTWOC = 'btwoc'
 

Static Public Attributes

static $useOpenssl = true
 

Protected Member Functions

 convert ($number, $inputFormat=self::FORMAT_NUMBER, $outputFormat=self::FORMAT_BINARY)
 Convert number between formats.
 
 generatePrivateKey ()
 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.
 

Protected Attributes

 $opensslKeyResource = null
 

Detailed Description

PHP implementation of the Diffie-Hellman public key encryption algorithm.

Allows two unassociated parties to establish a joint shared secret key to be used in encrypting subsequent communications.

Constructor & Destructor Documentation

__construct (   $prime,
  $generator,
  $privateKey = null,
  $privateKeyFormat = self::FORMAT_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$privateKeyFormat

Member Function Documentation

computeSecretKey (   $publicKey,
  $publicKeyFormat = self::FORMAT_NUMBER,
  $secretKeyFormat = self::FORMAT_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$publicKeyFormat
string$secretKeyFormat
Returns
string
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
\Zend\Crypt\Exception\RuntimeException
convert (   $number,
  $inputFormat = self::FORMAT_NUMBER,
  $outputFormat = self::FORMAT_BINARY 
)
protected

Convert number between formats.

Parameters
string$number
string$inputFormat
string$outputFormat
Returns
string
generateKeys ( )

Generate own public key.

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

Returns
DiffieHellman
Exceptions
\Zend\Crypt\Exception\RuntimeException
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
getGenerator (   $format = self::FORMAT_NUMBER)

Getter for the value of the generator number.

Parameters
string$format
Returns
string
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
getPrime (   $format = self::FORMAT_NUMBER)

Getter for the value of the prime number.

Parameters
string$format
Returns
string
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
getPrivateKey (   $format = self::FORMAT_NUMBER)

Getter for the value of the private number.

Parameters
string$format
Returns
string
getPublicKey (   $format = self::FORMAT_NUMBER)

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

Parameters
string$format
Returns
string
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
getSharedSecretKey (   $format = self::FORMAT_NUMBER)

Return the computed shared secret key from the DiffieHellman transaction.

Parameters
string$format
Returns
string
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
hasPrivateKey ( )

Check whether a private key currently exists.

Returns
bool
setGenerator (   $number)

Setter for the value of the generator number.

Parameters
string$number
Returns
DiffieHellman
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
setPrime (   $number)

Setter for the value of the prime number.

Parameters
string$number
Returns
DiffieHellman
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
setPrivateKey (   $number,
  $format = self::FORMAT_NUMBER 
)

Setter for the value of the private number.

Parameters
string$number
string$format
Returns
DiffieHellman
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
setPublicKey (   $number,
  $format = self::FORMAT_NUMBER 
)

Setter for the value of the public number.

Parameters
string$number
string$format
Returns
DiffieHellman
Exceptions
\Zend\Crypt\Exception\InvalidArgumentException
static useOpensslExtension (   $flag = true)
static

Set whether to use openssl extension.

Parameters
bool$flag

Member Data Documentation

$opensslKeyResource = null
protected
$useOpenssl = true
static
const DEFAULT_KEY_SIZE = 2048
const FORMAT_BINARY = 'binary'

Key formats.

const FORMAT_BTWOC = 'btwoc'
const FORMAT_NUMBER = 'number'