PHP code example of querdos / php-dh

1. Go to this page and download the library: Download querdos/php-dh library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

querdos / php-dh example snippets



Querdos\DiffieHellman;

// alice and bob generate their data and exchange public ones
$dh_bob   = new DiffieHellman();
$dh_alice = new DiffieHellman(null, $dh_bob->getModulus(), $dh_bob->getBase());

// first initialization for both alice and bob (private and public keys generation)
$dh_bob->init();
$dh_alice->init();

// generating a shared secret with corresponding public keys
$dh_alice->compute_secret($dh_bob->getPublic());
$dh_bob->compute_secret($dh_alice->getPublic());

if (0 == gmp_cmp($dh_alice->getSecret(), $dh_bob->getSecret())) {
    echo "Alice and Bob share the same secret" . PHP_EOL;
} else {
    echo "Alice and Bob don't share the same secret" . PHP_EOL;
}


$dh = new DiffieHellman();                      // no value, 1536bits by default
$dh = new DiffieHellman(self::PREDEFINED_1536); // 1536bits length
$dh = new DiffieHellman(self::PREDEFINED_3072); // 3072bits length 
$dh = new DiffieHellman(self::PREDEFINED_4096); // 4096bits length
$dh = new DiffieHellman(self::PREDEFINED_6144); // 6144bits length
$dh = new DiffieHellman(self::PREDEFINED_8192); // 8192bits length
bash
# Debian like installation
$ sudo apt-get install php-gmp
bash
$ composer