/Classes/Security/Cryptography/HashService.php

https://github.com/arbyte/FLOW3-X-TYPO3.FLOW3 · PHP · 132 lines · 39 code · 16 blank · 77 comment · 3 complexity · cd68b6cf69cba26460df669f419113a4 MD5 · raw file

  1. <?php
  2. namespace TYPO3\FLOW3\Security\Cryptography;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * A hash service which should be used to generate and validate hashes.
  15. *
  16. * @FLOW3\Scope("singleton")
  17. */
  18. class HashService {
  19. /**
  20. * A private, unique key used for encryption tasks.
  21. * @var string
  22. */
  23. protected $encryptionKey;
  24. /**
  25. * @FLOW3\Inject
  26. * @var \TYPO3\FLOW3\Security\Cryptography\PasswordHashingStrategyInterface
  27. */
  28. protected $passwordHashingStrategy;
  29. /**
  30. */
  31. public function __construct() {
  32. if (!file_exists(FLOW3_PATH_DATA . 'Persistent/EncryptionKey')) {
  33. file_put_contents(FLOW3_PATH_DATA . 'Persistent/EncryptionKey', bin2hex(\TYPO3\FLOW3\Utility\Algorithms::generateRandomBytes(96)));
  34. }
  35. $this->encryptionKey = file_get_contents(FLOW3_PATH_DATA . 'Persistent/EncryptionKey');
  36. if (empty($this->encryptionKey)) {
  37. throw new \TYPO3\FLOW3\Security\Exception\MissingConfigurationException('No encryption key for the HashService was found and none could be created at "' . FLOW3_PATH_DATA . 'Persistent/EncryptionKey"', 1258991855);
  38. }
  39. }
  40. /**
  41. * Generate a hash (HMAC) for a given string
  42. *
  43. * @param string $string The string for which a hash should be generated
  44. * @return string The hash of the string
  45. * @throws TYPO3\FLOW3\Security\Exception\InvalidArgumentForHashGenerationException if something else than a string was given as parameter
  46. * @todo Mark as API once it is more stable
  47. */
  48. public function generateHmac($string) {
  49. if (!is_string($string)) throw new \TYPO3\FLOW3\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be generated for a string, but "' . gettype($string) . '" was given.', 1255069587);
  50. return hash_hmac('sha1', $string, $this->encryptionKey);
  51. }
  52. /**
  53. * Tests if a string $string matches the HMAC given by $hash.
  54. *
  55. * @param string $string The string which should be validated
  56. * @param string $hmac The hash of the string
  57. * @return boolean TRUE if string and hash fit together, FALSE otherwise.
  58. * @todo Mark as API once it is more stable
  59. */
  60. public function validateHmac($string, $hmac) {
  61. return ($this->generateHmac($string) === $hmac);
  62. }
  63. /**
  64. * Generates a salted md5 hash over the given string.
  65. *
  66. * @param string $clearString The unencrypted string which is the subject to be hashed
  67. * @return string Salted hash and the salt, separated by a comma ","
  68. * @deprecated Use hashPassword(...) instead
  69. */
  70. public function generateSaltedMd5($clearString) {
  71. return \TYPO3\FLOW3\Security\Cryptography\SaltedMd5HashingStrategy::generateSaltedMd5($clearString);
  72. }
  73. /**
  74. * Tests if the given string would produce the same hash given the specified salt.
  75. * Use this method to validate hashes generated with generateSlatedMd5().
  76. *
  77. * @param string $clearString
  78. * @param string $hashedStringAndSalt
  79. * @return boolean TRUE if the clear string matches, otherwise FALSE
  80. * @deprecated Use validatePassword(...) instead
  81. */
  82. public function validateSaltedMd5($clearString, $hashedStringAndSalt) {
  83. return \TYPO3\FLOW3\Security\Cryptography\SaltedMd5HashingStrategy::validateSaltedMd5($clearString, $hashedStringAndSalt);
  84. }
  85. /**
  86. * Hash a password using the configured password hashing strategy
  87. *
  88. * @param string $password The cleartext password
  89. * @return string A hashed password with salt (if used)
  90. * @api
  91. */
  92. public function hashPassword($password) {
  93. return $this->passwordHashingStrategy->hashPassword($password, $this->encryptionKey);
  94. }
  95. /**
  96. * Validate a hashed password using the configured password hashing strategy
  97. *
  98. * @param string $password The cleartext password
  99. * @param string $hashedPasswordAndSalt The hashed password with salt (if used)
  100. * @return boolean TRUE if the given password matches the hashed password
  101. * @api
  102. */
  103. public function validatePassword($password, $hashedPasswordAndSalt) {
  104. return $this->passwordHashingStrategy->validatePassword($password, $hashedPasswordAndSalt, $this->encryptionKey);
  105. }
  106. /**
  107. * Inject the password hashing strategy
  108. *
  109. * @param \TYPO3\FLOW3\Security\Cryptography\PasswordHashingStrategyInterface $passwordHashingStrategy
  110. * @return void
  111. */
  112. public function setPasswordHashingStrategy(\TYPO3\FLOW3\Security\Cryptography\PasswordHashingStrategyInterface $passwordHashingStrategy) {
  113. $this->passwordHashingStrategy = $passwordHashingStrategy;
  114. }
  115. }
  116. ?>