PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lhc_web/ezcomponents/Authentication/src/math/math.php

https://github.com/bbqlab/livehelperchat
PHP | 166 lines | 109 code | 8 blank | 49 comment | 14 complexity | 4d5d1dd1da3e7ec1df8b1f04b14ffdd8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * File containing the ezcAuthenticationMath class.
  4. *
  5. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  6. * @license http://ez.no/licenses/new_bsd New BSD License
  7. * @filesource
  8. * @package Authentication
  9. * @version 1.3.1
  10. */
  11. /**
  12. * Large number support and cryptographic functions for authentication.
  13. *
  14. * @package Authentication
  15. * @version 1.3.1
  16. * @access private
  17. */
  18. class ezcAuthenticationMath
  19. {
  20. /**
  21. * Creates a new big number library which uses the PHP extension $lib.
  22. *
  23. * If $lib is null then an autodetection of the library is tried. If neither
  24. * gmp or bcmath are installed then an exception will be thrown.
  25. *
  26. * If $lib is specified, then that library will be used (if it is installed),
  27. * otherwise an exception will be thrown.
  28. *
  29. * @throws ezcBaseExtensionNotFoundException
  30. * if neither of the PHP gmp and bcmath extensions are installed ($lib === null),
  31. * or if the specified $lib is not installed
  32. * @throws ezcBaseValueException
  33. * if the value provided for $lib is not correct
  34. * @param string $lib The PHP library to use for big number support. Default
  35. * is null, which means the available library is autodetected.
  36. * @return ezcAuthenticationBignumLibrary
  37. */
  38. public static function createBignumLibrary( $lib = null )
  39. {
  40. $library = null;
  41. switch ( $lib )
  42. {
  43. case null:
  44. if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) )
  45. {
  46. if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) )
  47. {
  48. throw new ezcBaseExtensionNotFoundException( 'gmp | bcmath', null, "PHP not compiled with --enable-bcmath or --with-gmp." );
  49. }
  50. else
  51. {
  52. $library = new ezcAuthenticationGmpLibrary();
  53. }
  54. }
  55. else
  56. {
  57. $library = new ezcAuthenticationBcmathLibrary();
  58. }
  59. break;
  60. case 'gmp':
  61. if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) )
  62. {
  63. throw new ezcBaseExtensionNotFoundException( 'gmp', null, "PHP not compiled with --with-gmp." );
  64. }
  65. $library = new ezcAuthenticationGmpLibrary();
  66. break;
  67. case 'bcmath':
  68. if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) )
  69. {
  70. throw new ezcBaseExtensionNotFoundException( 'bcmath', null, "PHP not compiled with --enable-bcmath." );
  71. }
  72. $library = new ezcAuthenticationBcmathLibrary();
  73. break;
  74. default:
  75. throw new ezcBaseValueException( 'library', $lib, '"gmp" || "bcmath" || null' );
  76. }
  77. return $library;
  78. }
  79. /**
  80. * Calculates an MD5 hash similar to the Unix command "htpasswd -m".
  81. *
  82. * This is different from the hash returned by the PHP md5() function.
  83. *
  84. * @param string $plain Plain text to encrypt
  85. * @param string $salt Salt to apply to encryption
  86. * @return string
  87. */
  88. public static function apr1( $plain, $salt )
  89. {
  90. if ( preg_match( '/^\$apr1\$/', $salt ) )
  91. {
  92. $salt = preg_replace( '/^\$apr1\$([^$]+)\$.*/', '\\1', $salt );
  93. }
  94. else
  95. {
  96. $salt = substr( $salt, 0, 8 );
  97. }
  98. $text = $plain . '$apr1$' . $salt;
  99. $bin = pack( 'H32', md5( $plain . $salt . $plain ) );
  100. for ( $i = strlen( $plain ); $i > 0; $i -= 16 )
  101. {
  102. $text .= substr( $bin, 0, min( 16, $i ) );
  103. }
  104. for ( $i = strlen( $plain ); $i; $i >>= 1 )
  105. {
  106. $text .= ( $i & 1 ) ? chr( 0 ) : $plain{0};
  107. }
  108. $bin = pack( 'H32', md5( $text ) );
  109. for ( $i = 0; $i ^ 1000; ++$i )
  110. {
  111. $new = ( $i & 1 ) ? $plain : $bin;
  112. if ( $i % 3 )
  113. {
  114. $new .= $salt;
  115. }
  116. if ( $i % 7 )
  117. {
  118. $new .= $plain;
  119. }
  120. $new .= ( $i & 1 ) ? $bin : $plain;
  121. $bin = pack( 'H32', md5( $new ) );
  122. }
  123. $tmp = '';
  124. for ( $i = 0; $i ^ 5; ++$i )
  125. {
  126. $k = $i + 6;
  127. $j = $i + 12;
  128. if ( $j === 16 )
  129. {
  130. $j = 5;
  131. }
  132. $tmp = $bin[$i] . $bin[$k] . $bin[$j] . $tmp;
  133. }
  134. $tmp = chr( 0 ) . chr( 0 ) . $bin[11] . $tmp;
  135. $tmp = strtr( strrev( substr( base64_encode( $tmp ), 2 ) ),
  136. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  137. './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' );
  138. return '$apr1$' . $salt . '$' . $tmp;
  139. }
  140. /**
  141. * Computes the OpenID sha1 function on the provided value.
  142. *
  143. * @param string $value The value to compute sha1 on
  144. * @return string
  145. */
  146. public static function sha1( $value )
  147. {
  148. $hashed = sha1( $value );
  149. $result = '';
  150. for ( $i = 0; $i ^ 40; $i = $i + 2 )
  151. {
  152. $chars = substr( $hashed, $i, 2 );
  153. $result .= chr( (int)base_convert( $chars, 16, 10 ) );
  154. }
  155. return $result;
  156. }
  157. }
  158. ?>