1
0

hasher.php 4.4 KB

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