PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/pearlib/RSACrypt/Crypt/RSA/MathLoader.php

https://github.com/drobbins/s3db
PHP | 133 lines | 49 code | 5 blank | 79 comment | 6 complexity | a148fa1773b90ff45ef48bb38e12fc09 MD5 | raw file
  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 2005, 2006 Alexander Valyalkin
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version 1.2.0b
  22. * @link http://pear.php.net/package/Crypt_RSA
  23. */
  24. /**
  25. * RSA error handling facilities
  26. */
  27. require_once 'ErrorHandler.php';
  28. /**
  29. * Crypt_RSA_MathLoader class.
  30. *
  31. * Provides static function:
  32. * - loadWrapper($wrapper_name) - loads RSA math wrapper with name $wrapper_name
  33. * or most suitable wrapper if $wrapper_name == 'default'
  34. *
  35. * Example usage:
  36. * // load BigInt wrapper
  37. * $big_int_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BigInt');
  38. *
  39. * // load BCMath wrapper
  40. * $bcmath_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BCMath');
  41. *
  42. * // load the most suitable wrapper
  43. * $bcmath_wrapper = &Crypt_RSA_MathLoader::loadWrapper();
  44. *
  45. * @category Encryption
  46. * @package Crypt_RSA
  47. * @author Alexander Valyalkin <valyala@gmail.com>
  48. * @copyright 2005, 2006 Alexander Valyalkin
  49. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  50. * @link http://pear.php.net/package/Crypt_RSA
  51. * @version @package_version@
  52. * @access public
  53. */
  54. class Crypt_RSA_MathLoader
  55. {
  56. /**
  57. * Loads RSA math wrapper with name $wrapper_name.
  58. * Implemented wrappers can be found at Crypt/RSA/Math folder.
  59. * Read docs/Crypt_RSA/docs/math_wrappers.txt for details
  60. *
  61. * This is a static function:
  62. * // load BigInt wrapper
  63. * $big_int_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BigInt');
  64. *
  65. * // load BCMath wrapper
  66. * $bcmath_wrapper = &Crypt_RSA_MathLoader::loadWrapper('BCMath');
  67. *
  68. * @param string $wrapper_name
  69. * @return object
  70. * Reference to object of wrapper with name $wrapper_name on success
  71. * or PEAR_Error object on error
  72. *
  73. * @access public
  74. */
  75. function &loadWrapper($wrapper_name = 'default')
  76. {
  77. static $math_objects = array();
  78. // ordered by performance. GMP is the fastest math library, BCMath - the slowest.
  79. static $math_wrappers = array('GMP', 'BigInt', 'BCMath',);
  80. if (isset($math_objects[$wrapper_name])) {
  81. /*
  82. wrapper with name $wrapper_name is already loaded and created.
  83. Return reference to existing copy of wrapper
  84. */
  85. return $math_objects[$wrapper_name];
  86. }
  87. $err_handler = &new Crypt_RSA_ErrorHandler;
  88. if ($wrapper_name === 'default') {
  89. // try to load the most suitable wrapper
  90. $n = sizeof($math_wrappers);
  91. for ($i = 0; $i < $n; $i++) {
  92. $obj = &Crypt_RSA_MathLoader::loadWrapper($math_wrappers[$i]);
  93. if (!$err_handler->isError($obj)) {
  94. // wrapper for $math_wrappers[$i] successfully loaded
  95. // register it as default wrapper and return reference to it
  96. return $math_objects['default'] = &$obj;
  97. }
  98. }
  99. // can't load any wrapper
  100. $err_handler->pushError("can't load any wrapper for existing math libraries", CRYPT_RSA_ERROR_NO_WRAPPERS);
  101. return $err_handler->getLastError();
  102. }
  103. $class_name = 'Crypt_RSA_Math_' . $wrapper_name;
  104. $class_filename = dirname(__FILE__) . '/Math/' . $wrapper_name . '.php';
  105. if (!is_file($class_filename)) {
  106. $err_handler->pushError("can't find file [{$class_filename}] for RSA math wrapper [{$wrapper_name}]", CRYPT_RSA_ERROR_NO_FILE);
  107. return $err_handler->getLastError();
  108. }
  109. require_once($class_filename);
  110. if (!class_exists($class_name)) {
  111. $err_handler->pushError("can't find class [{$class_name}] in file [{$class_filename}]", CRYPT_RSA_ERROR_NO_CLASS);
  112. return $err_handler->getLastError();
  113. }
  114. // create and return wrapper object on success or PEAR_Error object on error
  115. $obj = &new $class_name;
  116. if ($obj->errstr) {
  117. // cannot load required extension for math wrapper
  118. $err_handler->pushError($obj->errstr, CRYPT_RSA_ERROR_NO_EXT);
  119. return $err_handler->getLastError();
  120. }
  121. return $math_objects[$wrapper_name] = &$obj;
  122. }
  123. }
  124. ?>