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

/library/Zend/Crypt.php

https://bitbucket.org/nosen/jelly2
PHP | 168 lines | 95 code | 11 blank | 62 comment | 12 complexity | b5e7c8c97521073572388c10afed48cc 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-2011 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 23953 2011-05-03 05:47:39Z ralph $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Crypt
  24. * @copyright Copyright (c) 2005-2011 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. * @return unknown
  74. */
  75. public static function hash($algorithm, $data, $binaryOutput = false)
  76. {
  77. $algorithm = strtolower($algorithm);
  78. if (function_exists($algorithm)) {
  79. return $algorithm($data, $binaryOutput);
  80. }
  81. self::_detectHashSupport($algorithm);
  82. $supportedMethod = '_digest' . ucfirst(self::$_type);
  83. $result = self::$supportedMethod($algorithm, $data, $binaryOutput);
  84. return $result;
  85. }
  86. /**
  87. * @param string $algorithm
  88. * @throws Zend_Crypt_Exception
  89. */
  90. protected static function _detectHashSupport($algorithm)
  91. {
  92. if (function_exists('hash')) {
  93. self::$_type = self::TYPE_HASH;
  94. if (in_array($algorithm, hash_algos())) {
  95. return;
  96. }
  97. }
  98. if (function_exists('mhash')) {
  99. self::$_type = self::TYPE_MHASH;
  100. if (in_array($algorithm, self::$_supportedAlgosMhash)) {
  101. return;
  102. }
  103. }
  104. if (function_exists('openssl_digest')) {
  105. if ($algorithm == 'ripemd160') {
  106. $algorithm = 'rmd160';
  107. }
  108. self::$_type = self::TYPE_OPENSSL;
  109. if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
  110. return;
  111. }
  112. }
  113. /**
  114. * @see Zend_Crypt_Exception
  115. */
  116. require_once 'Zend/Crypt/Exception.php';
  117. throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
  118. }
  119. /**
  120. * @param string $algorithm
  121. * @param string $data
  122. * @param bool $binaryOutput
  123. * @return string
  124. */
  125. protected static function _digestHash($algorithm, $data, $binaryOutput)
  126. {
  127. return hash($algorithm, $data, $binaryOutput);
  128. }
  129. /**
  130. * @param string $algorithm
  131. * @param string $data
  132. * @param bool $binaryOutput
  133. * @return string
  134. */
  135. protected static function _digestMhash($algorithm, $data, $binaryOutput)
  136. {
  137. $constant = constant('MHASH_' . strtoupper($algorithm));
  138. $binary = mhash($constant, $data);
  139. if ($binaryOutput) {
  140. return $binary;
  141. }
  142. return bin2hex($binary);
  143. }
  144. /**
  145. * @param string $algorithm
  146. * @param string $data
  147. * @param bool $binaryOutput
  148. * @return string
  149. */
  150. protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
  151. {
  152. if ($algorithm == 'ripemd160') {
  153. $algorithm = 'rmd160';
  154. }
  155. return openssl_digest($data, $algorithm, $binaryOutput);
  156. }
  157. }