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.

package Default

 Methods

Constructor; if set construct the object using the parameter array to set values for Prime, Generator and Private.

__construct(string $prime, string $generator, string $privateKey = null, string $privateKeyFormat = self::FORMAT_NUMBER

If a Private Key is not set, one will be generated at random.

Parameters

$prime

string

$generator

string

$privateKey

string

$privateKeyFormat

string

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.

computeSecretKey(string $publicKey, string $publicKeyFormat = self::FORMAT_NUMBER, string $secretKeyFormat = self::FORMAT_NUMBER) : string

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

$publicKey

string

$publicKeyFormat

string

$secretKeyFormat

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException
\Zend\Crypt\Exception\RuntimeException

Returns

string

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

generateKeys() : \Zend\Crypt\PublicKey\DiffieHellman

Exceptions

\Zend\Crypt\Exception\RuntimeException

Returns

\Zend\Crypt\PublicKey\DiffieHellman

Getter for the value of the generator number

getGenerator(string $format = self::FORMAT_NUMBER) : string

Parameters

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

string

Getter for the value of the prime number

getPrime(string $format = self::FORMAT_NUMBER) : string

Parameters

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

string

Getter for the value of the private number

getPrivateKey(string $format = self::FORMAT_NUMBER) : string

Parameters

$format

string

Returns

string

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

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

Parameters

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

string

Return the computed shared secret key from the DiffieHellman transaction

getSharedSecretKey(string $format = self::FORMAT_NUMBER) : string

Parameters

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

string

Check whether a private key currently exists.

hasPrivateKey() : boolean

Returns

boolean

Setter for the value of the generator number

setGenerator(string $number) : \Zend\Crypt\PublicKey\DiffieHellman

Parameters

$number

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

\Zend\Crypt\PublicKey\DiffieHellman

Setter for the value of the prime number

setPrime(string $number) : \Zend\Crypt\PublicKey\DiffieHellman

Parameters

$number

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

\Zend\Crypt\PublicKey\DiffieHellman

Setter for the value of the private number

setPrivateKey(string $number, string $format = self::FORMAT_NUMBER) : \Zend\Crypt\PublicKey\DiffieHellman

Parameters

$number

string

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

\Zend\Crypt\PublicKey\DiffieHellman

Setter for the value of the public number

setPublicKey(string $number, string $format = self::FORMAT_NUMBER) : \Zend\Crypt\PublicKey\DiffieHellman

Parameters

$number

string

$format

string

Exceptions

\Zend\Crypt\Exception\InvalidArgumentException

Returns

\Zend\Crypt\PublicKey\DiffieHellman

Set whether to use openssl extension

useOpensslExtension(boolean $flag = true
Static
static

Parameters

$flag

boolean

Convert number between formats

convert(string $number, string $inputFormat = self::FORMAT_NUMBER, string $outputFormat = self::FORMAT_BINARY) : string

Parameters

$number

string

$inputFormat

string

$outputFormat

string

Returns

string

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.

generatePrivateKey() : string

Returns

string

 Properties

 

Static flag to select whether to use PHP5.3's openssl extension if available.

$useOpenssl : boolean

Default

true
Static
 

$opensslKeyResource

$opensslKeyResource : resource

Default

null
 

The default generator number. This number must be greater than 0 but less than the prime number set.

$generator : string

Default

null
 

BigInteger support object courtesy of Zend\Math

$math : \Zend\Math\BigInteger\Adapter\AdapterInterface

Default

null
 

Default large prime number; required by the algorithm.

$prime : string

Default

null
 

A private number set by the local user. It's optional and will be generated if not set.

$privateKey : string

Default

null
 

The public key generated by this instance after calling generateKeys().

$publicKey : string

Default

null
 

The shared secret key resulting from a completed Diffie Hellman exchange

$secretKey : string

Default

null

 Constants

 

DEFAULT_KEY_SIZE

DEFAULT_KEY_SIZE = 2048 
 

Key formats

FORMAT_BINARY = 'binary' 
 

FORMAT_BTWOC

FORMAT_BTWOC = 'btwoc' 
 

FORMAT_NUMBER

FORMAT_NUMBER = 'number'