ormpassword.class.inc.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. require_once(APPROOT.'/core/simplecrypt.class.inc.php');
  17. /**
  18. * ormPassword
  19. * encapsulate the behavior of a one way encrypted password stored hashed
  20. * with a per password (as random as possible) salt, in order to prevent a "Rainbow table" hack.
  21. * If a cryptographic random number generator is available (on Linux or Windows)
  22. * it will be used for generating the salt.
  23. *
  24. * @author Erwan Taloc <erwan.taloc@combodo.com>
  25. * @author Romain Quetiez <romain.quetiez@combodo.com>
  26. * @author Denis Flaven <denis.flaven@combodo.com>
  27. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  28. * @package itopORM
  29. */
  30. class ormPassword
  31. {
  32. protected $m_sHashed;
  33. protected $m_sSalt;
  34. /**
  35. * Constructor, initializes the password from the encrypted values
  36. */
  37. public function __construct($sHash = '', $sSalt = '')
  38. {
  39. $this->m_sHashed = $sHash;
  40. $this->m_sSalt = $sSalt;
  41. }
  42. /**
  43. * Encrypts the clear text password, with a unique salt
  44. */
  45. public function SetPassword($sClearTextPassword)
  46. {
  47. $this->m_sSalt = SimpleCrypt::GetNewSalt();
  48. $this->m_sHashed = $this->ComputeHash($sClearTextPassword);
  49. }
  50. /**
  51. * Print the password: displays some stars
  52. * @return string
  53. */
  54. public function __toString()
  55. {
  56. return '*****'; // Password can not be read
  57. }
  58. public function IsEmpty()
  59. {
  60. return ($this->m_hashed == null);
  61. }
  62. public function GetHash()
  63. {
  64. return $this->m_sHashed;
  65. }
  66. public function GetSalt()
  67. {
  68. return $this->m_sSalt;
  69. }
  70. /**
  71. * Displays the password: displays some stars
  72. * @return string
  73. */
  74. public function GetAsHTML()
  75. {
  76. return '*****'; // Password can not be read
  77. }
  78. /**
  79. * Check if the supplied clear text password matches the encrypted one
  80. * @param string $sClearTextPassword
  81. * @return boolean True if it matches, false otherwise
  82. */
  83. public function CheckPassword($sClearTextPassword)
  84. {
  85. $bResult = false;
  86. $sHashedPwd = $this->ComputeHash($sClearTextPassword);
  87. if ($this->m_sHashed == $sHashedPwd)
  88. {
  89. $bResult = true;
  90. }
  91. return $bResult;
  92. }
  93. /**
  94. * Computes the hashed version of a password using a unique salt
  95. * for this password. A unique salt is generated if needed
  96. * @return string
  97. */
  98. protected function ComputeHash($sClearTextPwd)
  99. {
  100. if ($this->m_sSalt == null)
  101. {
  102. $this->m_sSalt = SimpleCrypt::GetNewSalt();
  103. }
  104. return hash('sha256', $this->m_sSalt.$sClearTextPwd);
  105. }
  106. }
  107. ?>