Zend\Crypt\PublicKey\DiffieHellman
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.
Synopsis
class DiffieHellman
{
- // constants
- const DEFAULT_KEY_SIZE = 2048;
- const FORMAT_BINARY = 'binary';
- const FORMAT_NUMBER = 'number';
- const FORMAT_BTWOC = 'btwoc';
- // members
- public bool $useOpenssl;
- private string $prime;
- private string $generator;
- private string $privateKey;
- private AdapterInterface $math;
- private string $publicKey;
- private string $secretKey;
- protected resource $opensslKeyResource;
- // methods
- public void __construct()
- public static void useOpensslExtension()
- public DiffieHellman generateKeys()
- public DiffieHellman setPublicKey()
- public string getPublicKey()
- public string computeSecretKey()
- public string getSharedSecretKey()
- public DiffieHellman setPrime()
- public string getPrime()
- public DiffieHellman setGenerator()
- public string getGenerator()
- public DiffieHellman setPrivateKey()
- public string getPrivateKey()
- public bool hasPrivateKey()
- protected string convert()
- protected string generatePrivateKey()
Constants
Name | Value |
---|---|
DEFAULT_KEY_SIZE | 2048 |
FORMAT_BINARY | 'binary' |
FORMAT_NUMBER | 'number' |
FORMAT_BTWOC | 'btwoc' |
Members
private
- $generator
—
string
The default generator number. This number must be greater than 0 but less than the prime number set. - $math
—
\Zend\Math\BigInteger\Adapter\AdapterInterface
BigInteger support object courtesy of Zend\Math - $prime
—
string
Default large prime number; required by the algorithm. - $privateKey
—
string
A private number set by the local user. It's optional and will be generated if not set. - $publicKey
—
string
The public key generated by this instance after calling generateKeys(). - $secretKey
—
string
The shared secret key resulting from a completed Diffie Hellman exchange
protected
- $opensslKeyResource — resource
public
- $useOpenssl
—
Zend\Crypt\PublicKey\bool
Static flag to select whether to use PHP5.3's openssl extension if available.
Methods
protected
- convert() — 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. Having a random number generator installed on linux/bsd is highly recommended! The alternative is not recommended for production unless without any other option.
public
- __construct() — Constructor; if set construct the object using the parameter array to set values for Prime, Generator and Private.
- computeSecretKey() — 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.
- generateKeys() — Generate own public key. If a private number has not already been set, one will be generated at this stage.
- getGenerator() — Getter for the value of the generator number
- getPrime() — Getter for the value of the prime number
- getPrivateKey() — Getter for the value of the private number
- getPublicKey() — Returns own public key for communication to the second party to this transaction
- hasPrivateKey() — Check whether a private key currently exists.
- setGenerator() — Setter for the value of the generator number
- setPrime() — Setter for the value of the prime number
- setPrivateKey() — Setter for the value of the private number
- setPublicKey() — Setter for the value of the public number
- useOpensslExtension() — Set whether to use openssl extension