IHasher.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCP\Security;
  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 OCP\Security
  43. * @since 8.0.0
  44. */
  45. interface IHasher {
  46. /**
  47. * Hashes a message using PHP's `password_hash` functionality.
  48. * Please note that the size of the returned string is not guaranteed
  49. * and can be up to 255 characters.
  50. *
  51. * @param string $message Message to generate hash from
  52. * @return string Hash of the message with appended version parameter
  53. * @since 8.0.0
  54. */
  55. public function hash(string $message): string;
  56. /**
  57. * @param string $message Message to verify
  58. * @param string $hash Assumed hash of the message
  59. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  60. * @return bool Whether $hash is a valid hash of $message
  61. * @since 8.0.0
  62. */
  63. public function verify(string $message, string $hash, &$newHash = null): bool ;
  64. }