web/lib/Zend/Crypt/DiffieHellman.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Crypt
    16  * @package    Zend_Crypt
    17  * @subpackage DiffieHellman
    17  * @subpackage DiffieHellman
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: DiffieHellman.php 24593 2012-01-05 20:35:02Z matthew $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * PHP implementation of the Diffie-Hellman public key encryption algorithm.
    24  * PHP implementation of the Diffie-Hellman public key encryption algorithm.
    25  * Allows two unassociated parties to establish a joint shared secret key
    25  * Allows two unassociated parties to establish a joint shared secret key
    26  * to be used in encrypting subsequent communications.
    26  * to be used in encrypting subsequent communications.
    27  *
    27  *
    28  * @category   Zend
    28  * @category   Zend
    29  * @package    Zend_Crypt
    29  * @package    Zend_Crypt
    30  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    30  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  */
    32  */
    33 class Zend_Crypt_DiffieHellman
    33 class Zend_Crypt_DiffieHellman
    34 {
    34 {
    35 
    35 
   100      *
   100      *
   101      * @param string $prime
   101      * @param string $prime
   102      * @param string $generator
   102      * @param string $generator
   103      * @param string $privateKey
   103      * @param string $privateKey
   104      * @param string $privateKeyType
   104      * @param string $privateKeyType
   105      * @return void
       
   106      */
   105      */
   107     public function __construct($prime, $generator, $privateKey = null, $privateKeyType = self::NUMBER)
   106     public function __construct($prime, $generator, $privateKey = null, $privateKeyType = self::NUMBER)
   108     {
   107     {
   109         $this->setPrime($prime);
   108         $this->setPrime($prime);
   110         $this->setGenerator($generator);
   109         $this->setGenerator($generator);
   144     /**
   143     /**
   145      * Setter for the value of the public number
   144      * Setter for the value of the public number
   146      *
   145      *
   147      * @param string $number
   146      * @param string $number
   148      * @param string $type
   147      * @param string $type
       
   148      * @throws Zend_Crypt_DiffieHellman_Exception
   149      * @return Zend_Crypt_DiffieHellman
   149      * @return Zend_Crypt_DiffieHellman
   150      */
   150      */
   151     public function setPublicKey($number, $type = self::NUMBER)
   151     public function setPublicKey($number, $type = self::NUMBER)
   152     {
   152     {
   153         if ($type == self::BINARY) {
   153         if ($type == self::BINARY) {
   164     /**
   164     /**
   165      * Returns own public key for communication to the second party to this
   165      * Returns own public key for communication to the second party to this
   166      * transaction.
   166      * transaction.
   167      *
   167      *
   168      * @param string $type
   168      * @param string $type
       
   169      * @throws Zend_Crypt_DiffieHellman_Exception
   169      * @return string
   170      * @return string
   170      */
   171      */
   171     public function getPublicKey($type = self::NUMBER)
   172     public function getPublicKey($type = self::NUMBER)
   172     {
   173     {
   173         if ($this->_publicKey === null) {
   174         if ($this->_publicKey === null) {
   193      * If you need the binary form of the shared secret key, call
   194      * If you need the binary form of the shared secret key, call
   194      * getSharedSecretKey() with the optional parameter for Binary output.
   195      * getSharedSecretKey() with the optional parameter for Binary output.
   195      *
   196      *
   196      * @param string $publicKey
   197      * @param string $publicKey
   197      * @param string $type
   198      * @param string $type
       
   199      * @param string $output
       
   200      * @throws Zend_Crypt_DiffieHellman_Exception
   198      * @return mixed
   201      * @return mixed
   199      */
   202      */
   200     public function computeSecretKey($publicKey, $type = self::NUMBER, $output = self::NUMBER)
   203     public function computeSecretKey($publicKey, $type = self::NUMBER, $output = self::NUMBER)
   201     {
   204     {
   202         if ($type == self::BINARY) {
   205         if ($type == self::BINARY) {
   216 
   219 
   217     /**
   220     /**
   218      * Return the computed shared secret key from the DiffieHellman transaction
   221      * Return the computed shared secret key from the DiffieHellman transaction
   219      *
   222      *
   220      * @param string $type
   223      * @param string $type
       
   224      * @throws Zend_Crypt_DiffieHellman_Exception
   221      * @return string
   225      * @return string
   222      */
   226      */
   223     public function getSharedSecretKey($type = self::NUMBER)
   227     public function getSharedSecretKey($type = self::NUMBER)
   224     {
   228     {
   225         if (!isset($this->_secretKey)) {
   229         if (!isset($this->_secretKey)) {
   236 
   240 
   237     /**
   241     /**
   238      * Setter for the value of the prime number
   242      * Setter for the value of the prime number
   239      *
   243      *
   240      * @param string $number
   244      * @param string $number
       
   245      * @throws Zend_Crypt_DiffieHellman_Exception
   241      * @return Zend_Crypt_DiffieHellman
   246      * @return Zend_Crypt_DiffieHellman
   242      */
   247      */
   243     public function setPrime($number)
   248     public function setPrime($number)
   244     {
   249     {
   245         if (!preg_match("/^\d+$/", $number) || $number < 11) {
   250         if (!preg_match("/^\d+$/", $number) || $number < 11) {
   251     }
   256     }
   252 
   257 
   253     /**
   258     /**
   254      * Getter for the value of the prime number
   259      * Getter for the value of the prime number
   255      *
   260      *
       
   261      * @throws Zend_Crypt_DiffieHellman_Exception
   256      * @return string
   262      * @return string
   257      */
   263      */
   258     public function getPrime()
   264     public function getPrime()
   259     {
   265     {
   260         if (!isset($this->_prime)) {
   266         if (!isset($this->_prime)) {
   262             throw new Zend_Crypt_DiffieHellman_Exception('No prime number has been set');
   268             throw new Zend_Crypt_DiffieHellman_Exception('No prime number has been set');
   263         }
   269         }
   264         return $this->_prime;
   270         return $this->_prime;
   265     }
   271     }
   266 
   272 
   267 
       
   268     /**
   273     /**
   269      * Setter for the value of the generator number
   274      * Setter for the value of the generator number
   270      *
   275      *
   271      * @param string $number
   276      * @param string $number
       
   277      * @throws Zend_Crypt_DiffieHellman_Exception
   272      * @return Zend_Crypt_DiffieHellman
   278      * @return Zend_Crypt_DiffieHellman
   273      */
   279      */
   274     public function setGenerator($number)
   280     public function setGenerator($number)
   275     {
   281     {
   276         if (!preg_match("/^\d+$/", $number) || $number < 2) {
   282         if (!preg_match("/^\d+$/", $number) || $number < 2) {
   282     }
   288     }
   283 
   289 
   284     /**
   290     /**
   285      * Getter for the value of the generator number
   291      * Getter for the value of the generator number
   286      *
   292      *
       
   293      * @throws Zend_Crypt_DiffieHellman_Exception
   287      * @return string
   294      * @return string
   288      */
   295      */
   289     public function getGenerator()
   296     public function getGenerator()
   290     {
   297     {
   291         if (!isset($this->_generator)) {
   298         if (!isset($this->_generator)) {
   298     /**
   305     /**
   299      * Setter for the value of the private number
   306      * Setter for the value of the private number
   300      *
   307      *
   301      * @param string $number
   308      * @param string $number
   302      * @param string $type
   309      * @param string $type
       
   310      * @throws Zend_Crypt_DiffieHellman_Exception
   303      * @return Zend_Crypt_DiffieHellman
   311      * @return Zend_Crypt_DiffieHellman
   304      */
   312      */
   305     public function setPrivateKey($number, $type = self::NUMBER)
   313     public function setPrivateKey($number, $type = self::NUMBER)
   306     {
   314     {
   307         if ($type == self::BINARY) {
   315         if ($type == self::BINARY) {