hasher.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Security;
  23. use OCP\IConfig;
  24. use OCP\Security\IHasher;
  25. /**
  26. * Class Hasher provides some basic hashing functions. Furthermore, it supports legacy hashes
  27. * used by previous versions of ownCloud and helps migrating those hashes to newer ones.
  28. *
  29. * The hashes generated by this class are prefixed (version|hash) with a version parameter to allow possible
  30. * updates in the future.
  31. * Possible versions:
  32. * - 1 (Initial version)
  33. *
  34. * Usage:
  35. * // Hashing a message
  36. * $hash = \OC::$server->getHasher()->hash('MessageToHash');
  37. * // Verifying a message - $newHash will contain the newly calculated hash
  38. * $newHash = null;
  39. * var_dump(\OC::$server->getHasher()->verify('a', '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', $newHash));
  40. * var_dump($newHash);
  41. *
  42. * @package OC\Security
  43. */
  44. class Hasher implements IHasher {
  45. /** @var IConfig */
  46. private $config;
  47. /** @var array Options passed to password_hash and password_needs_rehash */
  48. private $options = array();
  49. /** @var string Salt used for legacy passwords */
  50. private $legacySalt = null;
  51. /** @var int Current version of the generated hash */
  52. private $currentVersion = 1;
  53. /**
  54. * @param IConfig $config
  55. */
  56. function __construct(IConfig $config) {
  57. $this->config = $config;
  58. $hashingCost = $this->config->getSystemValue('hashingCost', null);
  59. if(!is_null($hashingCost)) {
  60. $this->options['cost'] = $hashingCost;
  61. }
  62. }
  63. /**
  64. * Hashes a message using PHP's `password_hash` functionality.
  65. * Please note that the size of the returned string is not guaranteed
  66. * and can be up to 255 characters.
  67. *
  68. * @param string $message Message to generate hash from
  69. * @return string Hash of the message with appended version parameter
  70. */
  71. public function hash($message) {
  72. return $this->currentVersion . '|' . password_hash($message, PASSWORD_DEFAULT, $this->options);
  73. }
  74. /**
  75. * Get the version and hash from a prefixedHash
  76. * @param string $prefixedHash
  77. * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo')
  78. */
  79. protected function splitHash($prefixedHash) {
  80. $explodedString = explode('|', $prefixedHash, 2);
  81. if(sizeof($explodedString) === 2) {
  82. if((int)$explodedString[0] > 0) {
  83. return array('version' => (int)$explodedString[0], 'hash' => $explodedString[1]);
  84. }
  85. }
  86. return null;
  87. }
  88. /**
  89. * Verify legacy hashes
  90. * @param string $message Message to verify
  91. * @param string $hash Assumed hash of the message
  92. * @param null|string &$newHash Reference will contain the updated hash
  93. * @return bool Whether $hash is a valid hash of $message
  94. */
  95. protected function legacyHashVerify($message, $hash, &$newHash = null) {
  96. if(empty($this->legacySalt)) {
  97. $this->legacySalt = $this->config->getSystemValue('passwordsalt', '');
  98. }
  99. // Verify whether it matches a legacy PHPass or SHA1 string
  100. $hashLength = strlen($hash);
  101. if($hashLength === 60 && password_verify($message.$this->legacySalt, $hash) ||
  102. $hashLength === 40 && StringUtils::equals($hash, sha1($message))) {
  103. $newHash = $this->hash($message);
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Verify V1 hashes
  110. * @param string $message Message to verify
  111. * @param string $hash Assumed hash of the message
  112. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  113. * @return bool Whether $hash is a valid hash of $message
  114. */
  115. protected function verifyHashV1($message, $hash, &$newHash = null) {
  116. if(password_verify($message, $hash)) {
  117. if(password_needs_rehash($hash, PASSWORD_DEFAULT, $this->options)) {
  118. $newHash = $this->hash($message);
  119. }
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * @param string $message Message to verify
  126. * @param string $hash Assumed hash of the message
  127. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  128. * @return bool Whether $hash is a valid hash of $message
  129. */
  130. public function verify($message, $hash, &$newHash = null) {
  131. $splittedHash = $this->splitHash($hash);
  132. if(isset($splittedHash['version'])) {
  133. switch ($splittedHash['version']) {
  134. case 1:
  135. return $this->verifyHashV1($message, $splittedHash['hash'], $newHash);
  136. }
  137. } else {
  138. return $this->legacyHashVerify($message, $hash, $newHash);
  139. }
  140. return false;
  141. }
  142. }