]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/extlib/Crypt/RSA/MathLoader.php
generate keypairs for users, and put them in the XRD for discovery
[quix0rs-gnu-social.git] / plugins / OStatus / extlib / Crypt / RSA / MathLoader.php
1 <?php
2 /**
3  * Crypt_RSA allows to do following operations:
4  *     - key pair generation
5  *     - encryption and decryption
6  *     - signing and sign validation
7  *
8  * PHP versions 4 and 5
9  *
10  * LICENSE: This source file is subject to version 3.0 of the PHP license
11  * that is available through the world-wide-web at the following URI:
12  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13  * the PHP License and are unable to obtain it through the web, please
14  * send a note to license@php.net so we can mail you a copy immediately.
15  *
16  * @category  Encryption
17  * @package   Crypt_RSA
18  * @author    Alexander Valyalkin <valyala@gmail.com>
19  * @copyright Alexander Valyalkin 2005
20  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
21  * @version   CVS: $Id: MathLoader.php,v 1.5 2009/01/05 08:30:29 clockwerx Exp $
22  * @link      http://pear.php.net/package/Crypt_RSA
23  */
24
25 /**
26  * RSA error handling facilities
27  */
28 require_once 'Crypt/RSA/ErrorHandler.php';
29
30 /**
31  * Crypt_RSA_MathLoader class.
32  *
33  * Provides static function:
34  *  - loadWrapper($wrapper_name) - loads RSA math wrapper with name $wrapper_name
35  *                                 or most suitable wrapper if $wrapper_name == 'default'
36  *
37  * Example usage:
38  *    // load BigInt wrapper
39  *    $big_int_wrapper = Crypt_RSA_MathLoader::loadWrapper('BigInt');
40  * 
41  *    // load BCMath wrapper
42  *    $bcmath_wrapper = Crypt_RSA_MathLoader::loadWrapper('BCMath');
43  * 
44  *    // load the most suitable wrapper
45  *    $bcmath_wrapper = Crypt_RSA_MathLoader::loadWrapper();
46  * 
47  * @category  Encryption
48  * @package   Crypt_RSA
49  * @author    Alexander Valyalkin <valyala@gmail.com>
50  * @copyright Alexander Valyalkin 2005
51  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
52  * @version   Release: @package_version@
53  * @link      http://pear.php.net/package/Crypt_RSA
54  * @access    public
55  */
56 class Crypt_RSA_MathLoader
57 {
58     /**
59      * Loads RSA math wrapper with name $wrapper_name.
60      * Implemented wrappers can be found at Crypt/RSA/Math folder.
61      * Read docs/Crypt_RSA/docs/math_wrappers.txt for details
62      *
63      * This is a static function:
64      *    // load BigInt wrapper
65      *    $big_int_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BigInt');
66      *
67      *    // load BCMath wrapper
68      *    $bcmath_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BCMath');
69      *
70      * @param string $wrapper_name Name of wrapper
71      *
72      * @return object
73      *         Reference to object of wrapper with name $wrapper_name on success
74      *         or PEAR_Error object on error
75      *
76      * @access public
77      */
78     function loadWrapper($wrapper_name = 'default')
79     {
80         static $math_objects = array();
81         // ordered by performance. GMP is the fastest math library, BCMath - the slowest.
82         static $math_wrappers = array('GMP', 'BigInt', 'BCMath',);
83
84         if (isset($math_objects[$wrapper_name])) {
85             /*
86                 wrapper with name $wrapper_name is already loaded and created.
87                 Return reference to existing copy of wrapper
88             */
89             return $math_objects[$wrapper_name];
90         }
91
92         $err_handler = new Crypt_RSA_ErrorHandler();
93
94         if ($wrapper_name === 'default') {
95             // try to load the most suitable wrapper
96             $n = sizeof($math_wrappers);
97             for ($i = 0; $i < $n; $i++) {
98                 $obj = Crypt_RSA_MathLoader::loadWrapper($math_wrappers[$i]);
99                 if (!$err_handler->isError($obj)) {
100                     // wrapper for $math_wrappers[$i] successfully loaded
101                     // register it as default wrapper and return reference to it
102                     return $math_objects['default'] = $obj;
103                 }
104             }
105             // can't load any wrapper
106             $err_handler->pushError("can't load any wrapper for existing math libraries", CRYPT_RSA_ERROR_NO_WRAPPERS);
107             return $err_handler->getLastError();
108         }
109
110         $class_name = 'Crypt_RSA_Math_' . $wrapper_name;
111         $class_filename = dirname(__FILE__) . '/Math/' . $wrapper_name . '.php';
112
113         if (!is_file($class_filename)) {
114             $err_handler->pushError("can't find file [{$class_filename}] for RSA math wrapper [{$wrapper_name}]", CRYPT_RSA_ERROR_NO_FILE);
115             return $err_handler->getLastError();
116         }
117
118         include_once $class_filename;
119         if (!class_exists($class_name)) {
120             $err_handler->pushError("can't find class [{$class_name}] in file [{$class_filename}]", CRYPT_RSA_ERROR_NO_CLASS);
121             return $err_handler->getLastError();
122         }
123
124         // create and return wrapper object on success or PEAR_Error object on error
125         $obj = new $class_name;
126         if ($obj->errstr) {
127             // cannot load required extension for math wrapper
128             $err_handler->pushError($obj->errstr, CRYPT_RSA_ERROR_NO_EXT);
129             return $err_handler->getLastError();
130         }
131         return $math_objects[$wrapper_name] = $obj;
132     }
133 }
134
135 ?>