function __construct($name, DH $dh) { $this->name = $name; $this->dh = $dh; $this->priv = $dh->generatePrivate(); $this->pub = $dh->generatePublic($this->priv); }
} function generateShared($private, $public) { return gmp_powm($public, $private, $this->p); } function p() { return gmp_strval($this->p, 16); } function g($val = null) { if (is_string($val)) { $this->g = gmp_init($val, 16); } return gmp_strval($this->g, 16); } } // don't output if we're included into another script. if (!debug_backtrace()) { $dh = new DH(); $a = $dh->generatePrivate(); $b = $dh->generatePrivate(); $A = $dh->generatePublic($a); $B = $dh->generatePublic($b); $s = $dh->generateShared($a, $B); $s2 = $dh->generateShared($b, $A); print "A and B shared secrets match:\n"; print gmp_cmp($s, $s2) === 0 ? "Yes!\n\n" : "No :(\n\n"; print "Shared secret:\n"; print gmp_strval($s, 16) . "\n\n"; }