00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2007 by Anton E. Lebedevich * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 /* $Id: DiffieHellmanKeyPair.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00018 final class DiffieHellmanKeyPair implements KeyPair 00019 { 00020 private $private = null; 00021 private $public = null; 00022 private $parameters = null; 00023 00024 public function __construct(DiffieHellmanParameters $parameters) 00025 { 00026 $this->parameters = $parameters; 00027 } 00028 00032 public static function create(DiffieHellmanParameters $parameters) 00033 { 00034 return new self($parameters); 00035 } 00036 00040 public static function generate( 00041 DiffieHellmanParameters $parameters, 00042 RandomSource $randomSource 00043 ) 00044 { 00045 $result = new self($parameters); 00046 00047 $factory = $parameters->getModulus()->getFactory(); 00048 00049 $result->private = $factory->makeRandom( 00050 $parameters->getModulus(), 00051 $randomSource 00052 ); 00053 00054 $result->public = $parameters->getGen()->modPow( 00055 $result->private, 00056 $parameters->getModulus() 00057 ); 00058 00059 return $result; 00060 } 00061 00065 public function setPrivate(BigInteger $private) 00066 { 00067 $this->private = $private; 00068 return $this; 00069 } 00070 00074 public function getPrivate() 00075 { 00076 return $this->private; 00077 } 00078 00082 public function setPublic(BigInteger $public) 00083 { 00084 $this->public = $public; 00085 return $this; 00086 } 00087 00091 public function getPublic() 00092 { 00093 return $this->public; 00094 } 00095 00099 public function makeSharedKey(BigInteger $otherSitePublic) 00100 { 00101 Assert::brothers($this->private, $otherSitePublic); 00102 00103 return $otherSitePublic->modPow( 00104 $this->private, 00105 $this->parameters->getModulus() 00106 ); 00107 } 00108 } 00109 ?>