ICrypto.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 Crypto provides a high-level encryption layer using AES-CBC. If no key has been provided
  27. * it will use the secret defined in config.php as key. Additionally the message will be HMAC'd.
  28. *
  29. * Usage:
  30. * $encryptWithDefaultPassword = \OC::$server->getCrypto()->encrypt('EncryptedText');
  31. * $encryptWithCustomPassword = \OC::$server->getCrypto()->encrypt('EncryptedText', 'password');
  32. *
  33. * @package OCP\Security
  34. * @since 8.0.0
  35. */
  36. interface ICrypto {
  37. /**
  38. * @param string $message The message to authenticate
  39. * @param string $password Password to use (defaults to `secret` in config.php)
  40. * @return string Calculated HMAC
  41. * @since 8.0.0
  42. */
  43. public function calculateHMAC(string $message, string $password = ''): string;
  44. /**
  45. * Encrypts a value and adds an HMAC (Encrypt-Then-MAC)
  46. * @param string $plaintext
  47. * @param string $password Password to encrypt, if not specified the secret from config.php will be taken
  48. * @return string Authenticated ciphertext
  49. * @since 8.0.0
  50. */
  51. public function encrypt(string $plaintext, string $password = ''): string;
  52. /**
  53. * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac)
  54. * @param string $authenticatedCiphertext
  55. * @param string $password Password to encrypt, if not specified the secret from config.php will be taken
  56. * @return string plaintext
  57. * @throws \Exception If the HMAC does not match
  58. * @since 8.0.0
  59. */
  60. public function decrypt(string $authenticatedCiphertext, string $password = ''): string;
  61. }