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 00017 final class DiffieHellmanKeyPair implements KeyPair 00018 { 00019 private $private = null; 00020 private $public = null; 00021 private $parameters = null; 00022 00023 public function __construct(DiffieHellmanParameters $parameters) 00024 { 00025 $this->parameters = $parameters; 00026 } 00027 00031 public static function create(DiffieHellmanParameters $parameters) 00032 { 00033 return new self($parameters); 00034 } 00035 00039 public static function generate( 00040 DiffieHellmanParameters $parameters, 00041 RandomSource $randomSource 00042 ) 00043 { 00044 $result = new self($parameters); 00045 00046 $factory = $parameters->getModulus()->getFactory(); 00047 00048 $result->private = $factory->makeRandom( 00049 $parameters->getModulus(), 00050 $randomSource 00051 ); 00052 00053 $result->public = $parameters->getGen()->modPow( 00054 $result->private, 00055 $parameters->getModulus() 00056 ); 00057 00058 return $result; 00059 } 00060 00064 public function setPrivate(BigInteger $private) 00065 { 00066 $this->private = $private; 00067 return $this; 00068 } 00069 00073 public function getPrivate() 00074 { 00075 return $this->private; 00076 } 00077 00081 public function setPublic(BigInteger $public) 00082 { 00083 $this->public = $public; 00084 return $this; 00085 } 00086 00090 public function getPublic() 00091 { 00092 return $this->public; 00093 } 00094 00098 public function makeSharedKey(BigInteger $otherSitePublic) 00099 { 00100 Assert::brothers($this->private, $otherSitePublic); 00101 00102 return $otherSitePublic->modPow( 00103 $this->private, 00104 $this->parameters->getModulus() 00105 ); 00106 } 00107 } 00108 ?>