PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Crypt.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 167 lines | 94 code | 11 blank | 62 comment | 12 complexity | 0e72fd104506094fd2c7682b0b924542 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-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. */
  20. /**
  21. * @category Zend
  22. * @package Zend_Crypt
  23. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. class Zend_Crypt
  27. {
  28. const TYPE_OPENSSL = 'openssl';
  29. const TYPE_HASH = 'hash';
  30. const TYPE_MHASH = 'mhash';
  31. protected static $_type = null;
  32. /**
  33. * @var array
  34. */
  35. protected static $_supportedAlgosOpenssl = array(
  36. 'md2',
  37. 'md4',
  38. 'mdc2',
  39. 'rmd160',
  40. 'sha',
  41. 'sha1',
  42. 'sha224',
  43. 'sha256',
  44. 'sha384',
  45. 'sha512'
  46. );
  47. /**
  48. * @var array
  49. */
  50. protected static $_supportedAlgosMhash = array(
  51. 'adler32',
  52. 'crc32',
  53. 'crc32b',
  54. 'gost',
  55. 'haval128',
  56. 'haval160',
  57. 'haval192',
  58. 'haval256',
  59. 'md4',
  60. 'md5',
  61. 'ripemd160',
  62. 'sha1',
  63. 'sha256',
  64. 'tiger',
  65. 'tiger128',
  66. 'tiger160'
  67. );
  68. /**
  69. * @param string $algorithm
  70. * @param string $data
  71. * @param bool $binaryOutput
  72. * @return unknown
  73. */
  74. public static function hash($algorithm, $data, $binaryOutput = false)
  75. {
  76. $algorithm = strtolower($algorithm);
  77. if (function_exists($algorithm)) {
  78. return $algorithm($data, $binaryOutput);
  79. }
  80. self::_detectHashSupport($algorithm);
  81. $supportedMethod = '_digest' . ucfirst(self::$_type);
  82. $result = self::$supportedMethod($algorithm, $data, $binaryOutput);
  83. }
  84. /**
  85. * @param string $algorithm
  86. * @throws Zend_Crypt_Exception
  87. */
  88. protected static function _detectHashSupport($algorithm)
  89. {
  90. if (function_exists('hash')) {
  91. self::$_type = self::TYPE_HASH;
  92. if (in_array($algorithm, hash_algos())) {
  93. return;
  94. }
  95. }
  96. if (function_exists('mhash')) {
  97. self::$_type = self::TYPE_MHASH;
  98. if (in_array($algorithm, self::$_supportedAlgosMhash)) {
  99. return;
  100. }
  101. }
  102. if (function_exists('openssl_digest')) {
  103. if ($algorithm == 'ripemd160') {
  104. $algorithm = 'rmd160';
  105. }
  106. self::$_type = self::TYPE_OPENSSL;
  107. if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
  108. return;
  109. }
  110. }
  111. /**
  112. * @see Zend_Crypt_Exception
  113. */
  114. require_once 'Zend/Crypt/Exception.php';
  115. throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
  116. }
  117. /**
  118. * @param string $algorithm
  119. * @param string $data
  120. * @param bool $binaryOutput
  121. * @return string
  122. */
  123. protected static function _digestHash($algorithm, $data, $binaryOutput)
  124. {
  125. return hash($algorithm, $data, $binaryOutput);
  126. }
  127. /**
  128. * @param string $algorithm
  129. * @param string $data
  130. * @param bool $binaryOutput
  131. * @return string
  132. */
  133. protected static function _digestMhash($algorithm, $data, $binaryOutput)
  134. {
  135. $constant = constant('MHASH_' . strtoupper($algorithm));
  136. $binary = mhash($constant, $data);
  137. if ($binaryOutput) {
  138. return $binary;
  139. }
  140. return bin2hex($binary);
  141. }
  142. /**
  143. * @param string $algorithm
  144. * @param string $data
  145. * @param bool $binaryOutput
  146. * @return string
  147. */
  148. protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
  149. {
  150. if ($algorithm == 'ripemd160') {
  151. $algorithm = 'rmd160';
  152. }
  153. return openssl_digest($data, $algorithm, $binaryOutput);
  154. }
  155. }