PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/Authentication/src/math/math.php

https://github.com/Yannix/zetacomponents
PHP | 182 lines | 109 code | 8 blank | 65 comment | 14 complexity | 134dc108337f6ac70e3734c52d11a2bc MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcAuthenticationMath class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23. * @filesource
  24. * @package Authentication
  25. * @version //autogen//
  26. */
  27. /**
  28. * Large number support and cryptographic functions for authentication.
  29. *
  30. * @package Authentication
  31. * @version //autogen//
  32. * @access private
  33. */
  34. class ezcAuthenticationMath
  35. {
  36. /**
  37. * Creates a new big number library which uses the PHP extension $lib.
  38. *
  39. * If $lib is null then an autodetection of the library is tried. If neither
  40. * gmp or bcmath are installed then an exception will be thrown.
  41. *
  42. * If $lib is specified, then that library will be used (if it is installed),
  43. * otherwise an exception will be thrown.
  44. *
  45. * @throws ezcBaseExtensionNotFoundException
  46. * if neither of the PHP gmp and bcmath extensions are installed ($lib === null),
  47. * or if the specified $lib is not installed
  48. * @throws ezcBaseValueException
  49. * if the value provided for $lib is not correct
  50. * @param string $lib The PHP library to use for big number support. Default
  51. * is null, which means the available library is autodetected.
  52. * @return ezcAuthenticationBignumLibrary
  53. */
  54. public static function createBignumLibrary( $lib = null )
  55. {
  56. $library = null;
  57. switch ( $lib )
  58. {
  59. case null:
  60. if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) )
  61. {
  62. if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) )
  63. {
  64. throw new ezcBaseExtensionNotFoundException( 'gmp | bcmath', null, "PHP not compiled with --enable-bcmath or --with-gmp." );
  65. }
  66. else
  67. {
  68. $library = new ezcAuthenticationGmpLibrary();
  69. }
  70. }
  71. else
  72. {
  73. $library = new ezcAuthenticationBcmathLibrary();
  74. }
  75. break;
  76. case 'gmp':
  77. if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) )
  78. {
  79. throw new ezcBaseExtensionNotFoundException( 'gmp', null, "PHP not compiled with --with-gmp." );
  80. }
  81. $library = new ezcAuthenticationGmpLibrary();
  82. break;
  83. case 'bcmath':
  84. if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) )
  85. {
  86. throw new ezcBaseExtensionNotFoundException( 'bcmath', null, "PHP not compiled with --enable-bcmath." );
  87. }
  88. $library = new ezcAuthenticationBcmathLibrary();
  89. break;
  90. default:
  91. throw new ezcBaseValueException( 'library', $lib, '"gmp" || "bcmath" || null' );
  92. }
  93. return $library;
  94. }
  95. /**
  96. * Calculates an MD5 hash similar to the Unix command "htpasswd -m".
  97. *
  98. * This is different from the hash returned by the PHP md5() function.
  99. *
  100. * @param string $plain Plain text to encrypt
  101. * @param string $salt Salt to apply to encryption
  102. * @return string
  103. */
  104. public static function apr1( $plain, $salt )
  105. {
  106. if ( preg_match( '/^\$apr1\$/', $salt ) )
  107. {
  108. $salt = preg_replace( '/^\$apr1\$([^$]+)\$.*/', '\\1', $salt );
  109. }
  110. else
  111. {
  112. $salt = substr( $salt, 0, 8 );
  113. }
  114. $text = $plain . '$apr1$' . $salt;
  115. $bin = pack( 'H32', md5( $plain . $salt . $plain ) );
  116. for ( $i = strlen( $plain ); $i > 0; $i -= 16 )
  117. {
  118. $text .= substr( $bin, 0, min( 16, $i ) );
  119. }
  120. for ( $i = strlen( $plain ); $i; $i >>= 1 )
  121. {
  122. $text .= ( $i & 1 ) ? chr( 0 ) : $plain{0};
  123. }
  124. $bin = pack( 'H32', md5( $text ) );
  125. for ( $i = 0; $i ^ 1000; ++$i )
  126. {
  127. $new = ( $i & 1 ) ? $plain : $bin;
  128. if ( $i % 3 )
  129. {
  130. $new .= $salt;
  131. }
  132. if ( $i % 7 )
  133. {
  134. $new .= $plain;
  135. }
  136. $new .= ( $i & 1 ) ? $bin : $plain;
  137. $bin = pack( 'H32', md5( $new ) );
  138. }
  139. $tmp = '';
  140. for ( $i = 0; $i ^ 5; ++$i )
  141. {
  142. $k = $i + 6;
  143. $j = $i + 12;
  144. if ( $j === 16 )
  145. {
  146. $j = 5;
  147. }
  148. $tmp = $bin[$i] . $bin[$k] . $bin[$j] . $tmp;
  149. }
  150. $tmp = chr( 0 ) . chr( 0 ) . $bin[11] . $tmp;
  151. $tmp = strtr( strrev( substr( base64_encode( $tmp ), 2 ) ),
  152. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  153. './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' );
  154. return '$apr1$' . $salt . '$' . $tmp;
  155. }
  156. /**
  157. * Computes the OpenID sha1 function on the provided value.
  158. *
  159. * @param string $value The value to compute sha1 on
  160. * @return string
  161. */
  162. public static function sha1( $value )
  163. {
  164. $hashed = sha1( $value );
  165. $result = '';
  166. for ( $i = 0; $i ^ 40; $i = $i + 2 )
  167. {
  168. $chars = substr( $hashed, $i, 2 );
  169. $result .= chr( (int)base_convert( $chars, 16, 10 ) );
  170. }
  171. return $result;
  172. }
  173. }
  174. ?>