PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/library/Zend/Crypt.php

http://github.com/frapi/frapi
PHP | 167 lines | 93 code | 11 blank | 63 comment | 12 complexity | c203a8b941edb1bb1725365f1f9fd47c MD5 | raw file
Possible License(s): BSD-2-Clause
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Crypt
  24. * @copyright Copyright (c) 2005-2010 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. }
  85. /**
  86. * @param string $algorithm
  87. * @throws Zend_Crypt_Exception
  88. */
  89. protected static function _detectHashSupport($algorithm)
  90. {
  91. if (function_exists('hash')) {
  92. self::$_type = self::TYPE_HASH;
  93. if (in_array($algorithm, hash_algos())) {
  94. return;
  95. }
  96. }
  97. if (function_exists('mhash')) {
  98. self::$_type = self::TYPE_MHASH;
  99. if (in_array($algorithm, self::$_supportedAlgosMhash)) {
  100. return;
  101. }
  102. }
  103. if (function_exists('openssl_digest')) {
  104. if ($algorithm == 'ripemd160') {
  105. $algorithm = 'rmd160';
  106. }
  107. self::$_type = self::TYPE_OPENSSL;
  108. if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
  109. return;
  110. }
  111. }
  112. /**
  113. * @see Zend_Crypt_Exception
  114. */
  115. // require_once 'Zend/Crypt/Exception.php';
  116. throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
  117. }
  118. /**
  119. * @param string $algorithm
  120. * @param string $data
  121. * @param bool $binaryOutput
  122. * @return string
  123. */
  124. protected static function _digestHash($algorithm, $data, $binaryOutput)
  125. {
  126. return hash($algorithm, $data, $binaryOutput);
  127. }
  128. /**
  129. * @param string $algorithm
  130. * @param string $data
  131. * @param bool $binaryOutput
  132. * @return string
  133. */
  134. protected static function _digestMhash($algorithm, $data, $binaryOutput)
  135. {
  136. $constant = constant('MHASH_' . strtoupper($algorithm));
  137. $binary = mhash($constant, $data);
  138. if ($binaryOutput) {
  139. return $binary;
  140. }
  141. return bin2hex($binary);
  142. }
  143. /**
  144. * @param string $algorithm
  145. * @param string $data
  146. * @param bool $binaryOutput
  147. * @return string
  148. */
  149. protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
  150. {
  151. if ($algorithm == 'ripemd160') {
  152. $algorithm = 'rmd160';
  153. }
  154. return openssl_digest($data, $algorithm, $binaryOutput);
  155. }
  156. }