4 * The OpenID library's Diffie-Hellman implementation.
8 * LICENSE: See the COPYING file included in this distribution.
12 * @author JanRain, Inc. <openid@janrain.com>
13 * @copyright 2005-2008 Janrain, Inc.
14 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
17 require_once 'Auth/OpenID.php';
18 require_once 'Auth/OpenID/BigMath.php';
20 function Auth_OpenID_getDefaultMod()
22 return '155172898181473697471232257763715539915724801'.
23 '966915404479707795314057629378541917580651227423'.
24 '698188993727816152646631438561595825688188889951'.
25 '272158842675419950341258706556549803580104870537'.
26 '681476726513255747040765857479291291572334510643'.
27 '245094715007229621094194349783925984760375594985'.
28 '848253359305585439638443';
31 function Auth_OpenID_getDefaultGen()
37 * The Diffie-Hellman key exchange class. This class relies on
38 * {@link Auth_OpenID_MathLibrary} to perform large number operations.
43 class Auth_OpenID_DiffieHellman {
50 function Auth_OpenID_DiffieHellman($mod = null, $gen = null,
51 $private = null, $lib = null)
54 $this->lib = Auth_OpenID_getMathLib();
60 $this->mod = $this->lib->init(Auth_OpenID_getDefaultMod());
66 $this->gen = $this->lib->init(Auth_OpenID_getDefaultGen());
71 if ($private === null) {
72 $r = $this->lib->rand($this->mod);
73 $this->private = $this->lib->add($r, 1);
75 $this->private = $private;
78 $this->public = $this->lib->powmod($this->gen, $this->private,
82 function getSharedSecret($composite)
84 return $this->lib->powmod($composite, $this->private, $this->mod);
87 function getPublicKey()
92 function usingDefaultValues()
94 return ($this->mod == Auth_OpenID_getDefaultMod() &&
95 $this->gen == Auth_OpenID_getDefaultGen());
98 function xorSecret($composite, $secret, $hash_func)
100 $dh_shared = $this->getSharedSecret($composite);
101 $dh_shared_str = $this->lib->longToBinary($dh_shared);
102 $hash_dh_shared = $hash_func($dh_shared_str);
105 for ($i = 0; $i < Auth_OpenID::bytes($secret); $i++) {
106 $xsecret .= chr(ord($secret[$i]) ^ ord($hash_dh_shared[$i]));