IHasher.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCP\Security;
  24. /**
  25. * Class Hasher provides some basic hashing functions. Furthermore, it supports legacy hashes
  26. * used by previous versions of ownCloud and helps migrating those hashes to newer ones.
  27. *
  28. * The hashes generated by this class are prefixed (version|hash) with a version parameter to allow possible
  29. * updates in the future.
  30. * Possible versions:
  31. * - 1 (Initial version)
  32. *
  33. * Usage:
  34. * // Hashing a message
  35. * $hash = \OC::$server->getHasher()->hash('MessageToHash');
  36. * // Verifying a message - $newHash will contain the newly calculated hash
  37. * $newHash = null;
  38. * var_dump(\OC::$server->getHasher()->verify('a', '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', $newHash));
  39. * var_dump($newHash);
  40. *
  41. * @package OCP\Security
  42. * @since 8.0.0
  43. */
  44. interface IHasher {
  45. /**
  46. * Hashes a message using PHP's `password_hash` functionality.
  47. * Please note that the size of the returned string is not guaranteed
  48. * and can be up to 255 characters.
  49. *
  50. * @param string $message Message to generate hash from
  51. * @return string Hash of the message with appended version parameter
  52. * @since 8.0.0
  53. */
  54. public function hash($message);
  55. /**
  56. * @param string $message Message to verify
  57. * @param string $hash Assumed hash of the message
  58. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  59. * @return bool Whether $hash is a valid hash of $message
  60. * @since 8.0.0
  61. */
  62. public function verify($message, $hash, &$newHash = null);
  63. }