/library/Zend/Crypt.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 176 lines · 95 code · 14 blank · 67 comment · 12 complexity · 0051e91157ab8df6b5c23482f61d8f41 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Crypt.php 25024 2012-07-30 15:08:15Z rob $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Crypt
  24. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Crypt
  28. {
  29. const TYPE_OPENSSL = 'openssl';
  30. const TYPE_HASH = 'hash';
  31. const TYPE_MHASH = 'mhash';
  32. protected static $_type = null;
  33. /**
  34. * @var array
  35. */
  36. protected static $_supportedAlgosOpenssl = array(
  37. 'md2',
  38. 'md4',
  39. 'mdc2',
  40. 'rmd160',
  41. 'sha',
  42. 'sha1',
  43. 'sha224',
  44. 'sha256',
  45. 'sha384',
  46. 'sha512'
  47. );
  48. /**
  49. * @var array
  50. */
  51. protected static $_supportedAlgosMhash = array(
  52. 'adler32',
  53. 'crc32',
  54. 'crc32b',
  55. 'gost',
  56. 'haval128',
  57. 'haval160',
  58. 'haval192',
  59. 'haval256',
  60. 'md4',
  61. 'md5',
  62. 'ripemd160',
  63. 'sha1',
  64. 'sha256',
  65. 'tiger',
  66. 'tiger128',
  67. 'tiger160'
  68. );
  69. /**
  70. * @param string $algorithm
  71. * @param string $data
  72. * @param bool $binaryOutput
  73. *
  74. * @return unknown
  75. */
  76. public static function hash ($algorithm, $data, $binaryOutput = false)
  77. {
  78. $algorithm = strtolower($algorithm);
  79. if (function_exists($algorithm)) {
  80. return $algorithm($data, $binaryOutput);
  81. }
  82. self::_detectHashSupport($algorithm);
  83. $supportedMethod = '_digest' . ucfirst(self::$_type);
  84. $result = self::$supportedMethod($algorithm, $data, $binaryOutput);
  85. return $result;
  86. }
  87. /**
  88. * @param string $algorithm
  89. *
  90. * @throws Zend_Crypt_Exception
  91. */
  92. protected static function _detectHashSupport ($algorithm)
  93. {
  94. if (function_exists('hash')) {
  95. self::$_type = self::TYPE_HASH;
  96. if (in_array($algorithm, hash_algos())) {
  97. return;
  98. }
  99. }
  100. if (function_exists('mhash')) {
  101. self::$_type = self::TYPE_MHASH;
  102. if (in_array($algorithm, self::$_supportedAlgosMhash)) {
  103. return;
  104. }
  105. }
  106. if (function_exists('openssl_digest')) {
  107. if ($algorithm == 'ripemd160') {
  108. $algorithm = 'rmd160';
  109. }
  110. self::$_type = self::TYPE_OPENSSL;
  111. if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
  112. return;
  113. }
  114. }
  115. /**
  116. * @see Zend_Crypt_Exception
  117. */
  118. require_once 'Zend/Crypt/Exception.php';
  119. throw new Zend_Crypt_Exception( '\'' . $algorithm . '\' is not supported by any available extension or native function' );
  120. }
  121. /**
  122. * @param string $algorithm
  123. * @param string $data
  124. * @param bool $binaryOutput
  125. *
  126. * @return string
  127. */
  128. protected static function _digestHash ($algorithm, $data, $binaryOutput)
  129. {
  130. return hash($algorithm, $data, $binaryOutput);
  131. }
  132. /**
  133. * @param string $algorithm
  134. * @param string $data
  135. * @param bool $binaryOutput
  136. *
  137. * @return string
  138. */
  139. protected static function _digestMhash ($algorithm, $data, $binaryOutput)
  140. {
  141. $constant = constant('MHASH_' . strtoupper($algorithm));
  142. $binary = mhash($constant, $data);
  143. if ($binaryOutput) {
  144. return $binary;
  145. }
  146. return bin2hex($binary);
  147. }
  148. /**
  149. * @param string $algorithm
  150. * @param string $data
  151. * @param bool $binaryOutput
  152. *
  153. * @return string
  154. */
  155. protected static function _digestOpenssl ($algorithm, $data, $binaryOutput)
  156. {
  157. if ($algorithm == 'ripemd160') {
  158. $algorithm = 'rmd160';
  159. }
  160. return openssl_digest($data, $algorithm, $binaryOutput);
  161. }
  162. }