PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/crypt/password/simple.php

https://bitbucket.org/eternaware/joomus
PHP | 148 lines | 69 code | 20 blank | 59 comment | 11 complexity | 726f52cdc97bb528526dce23c470551c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Crypt
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla Platform Password Crypter
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Crypt
  15. * @since 12.2
  16. */
  17. class JCryptPasswordSimple implements JCryptPassword
  18. {
  19. /**
  20. * @var integer The cost parameter for hashing algorithms.
  21. * @since 12.2
  22. */
  23. protected $cost = 10;
  24. /**
  25. * Creates a password hash
  26. *
  27. * @param string $password The password to hash.
  28. * @param string $type The hash type.
  29. *
  30. * @return string The hashed password.
  31. *
  32. * @since 12.2
  33. */
  34. public function create($password, $type = JCryptPassword::BLOWFISH)
  35. {
  36. switch ($type)
  37. {
  38. case JCryptPassword::BLOWFISH:
  39. $salt = $this->getSalt(22);
  40. if (version_compare(PHP_VERSION, '5.3.7') >= 0)
  41. {
  42. $prefix = '$2y$';
  43. }
  44. else
  45. {
  46. $prefix = '$2a$';
  47. }
  48. $salt = $prefix . str_pad($this->cost, 2, '0', STR_PAD_LEFT) . '$' . $this->getSalt(22);
  49. return crypt($password, $salt);
  50. case JCryptPassword::MD5:
  51. $salt = $this->getSalt(12);
  52. $salt = '$1$' . $salt;
  53. return crypt($password, $salt);
  54. case JCryptPassword::JOOMLA:
  55. $salt = $this->getSalt(32);
  56. return md5($password . $salt) . ':' . $salt;
  57. default:
  58. throw new InvalidArgumentException(sprintf('Hash type %s is not supported', $type));
  59. break;
  60. }
  61. }
  62. /**
  63. * Sets the cost parameter for the generated hash for algorithms that use a cost factor.
  64. *
  65. * @param integer $cost The new cost value.
  66. *
  67. * @return void
  68. *
  69. * @since 12.2
  70. */
  71. public function setCost($cost)
  72. {
  73. $this->cost = $cost;
  74. }
  75. /**
  76. * Generates a salt of specified length. The salt consists of characters in the set [./0-9A-Za-z].
  77. *
  78. * @param integer $length The number of characters to return.
  79. *
  80. * @return string The string of random characters.
  81. *
  82. * @since 12.2
  83. */
  84. protected function getSalt($length)
  85. {
  86. $bytes = ceil($length * 6 / 8);
  87. $randomData = str_replace('+', '.', base64_encode(JCrypt::getRandomBytes($bytes)));
  88. return substr($randomData, 0, $length);
  89. }
  90. /**
  91. * Verifies a password hash
  92. *
  93. * @param string $password The password to verify.
  94. * @param string $hash The password hash to check.
  95. *
  96. * @return boolean True if the password is valid, false otherwise.
  97. *
  98. * @since 12.2
  99. */
  100. public function verify($password, $hash)
  101. {
  102. // Check if the hash is a blowfish hash.
  103. if (substr($hash, 0, 4) == '$2a$' || substr($hash, 0, 4) == '$2y$')
  104. {
  105. if (version_compare(PHP_VERSION, '5.3.7') >= 0)
  106. {
  107. $prefix = '$2y$';
  108. }
  109. else
  110. {
  111. $prefix = '$2a$';
  112. }
  113. $hash = $prefix . substr($hash, 4);
  114. return (crypt($password, $hash) === $hash);
  115. }
  116. // Check if the hash is an MD5 hash.
  117. if (substr($hash, 0, 3) == '$1$')
  118. {
  119. return (crypt($password, $hash) === $hash);
  120. }
  121. // Check if the hash is a Joomla hash.
  122. if (preg_match('#[a-z0-9]{32}:[A-Za-z0-9]{32}#', $hash) === 1)
  123. {
  124. return md5($password . substr($hash, 33)) == substr($hash, 0, 32);
  125. }
  126. return false;
  127. }
  128. }