PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Security/Cryptography/SaltedMd5HashingStrategy.php

https://github.com/christianjul/FLOW3-Composer
PHP | 72 lines | 22 code | 7 blank | 43 comment | 1 complexity | f95227b25b18de1f90a22f1e54473ff8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  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. /**
  13. * A salted MD5 based password hashing strategy
  14. *
  15. */
  16. class SaltedMd5HashingStrategy implements \TYPO3\FLOW3\Security\Cryptography\PasswordHashingStrategyInterface {
  17. /**
  18. * Generates a salted md5 hash over the given string.
  19. *
  20. * @param string $clearString The unencrypted string which is the subject to be hashed
  21. * @return string Salted hash and the salt, separated by a comma ","
  22. */
  23. static public function generateSaltedMd5($clearString) {
  24. $salt = substr(md5(uniqid(rand(), TRUE)), 0, rand(6, 10));
  25. return (md5(md5($clearString) . $salt) . ',' . $salt);
  26. }
  27. /**
  28. * Tests if the given string would produce the same hash given the specified salt.
  29. * Use this method to validate hashes generated with generateSlatedMd5().
  30. *
  31. * @param string $clearString
  32. * @param string $hashedStringAndSalt
  33. * @return boolean TRUE if the clear string matches, otherwise FALSE
  34. * @throws \InvalidArgumentException
  35. */
  36. static public function validateSaltedMd5($clearString, $hashedStringAndSalt) {
  37. if (strpos($hashedStringAndSalt, ',') === FALSE) {
  38. throw new \InvalidArgumentException('The hashed string must contain a salt, separated with comma from the hashed.', 1269872776);
  39. }
  40. list($passwordHash, $salt) = explode(',', $hashedStringAndSalt);
  41. return (md5(md5($clearString) . $salt) === $passwordHash);
  42. }
  43. /**
  44. * Hash a password using salted MD5
  45. *
  46. * @param string $password The cleartext password
  47. * @param string $staticSalt ignored parameter
  48. * @return string A hashed password with salt
  49. */
  50. public function hashPassword($password, $staticSalt = NULL) {
  51. return self::generateSaltedMd5($password);
  52. }
  53. /**
  54. * Validate a hashed password using salted MD5
  55. *
  56. * @param string $password The cleartext password
  57. * @param string $hashedPasswordAndSalt The hashed password with salt
  58. * @param string $staticSalt ignored parameter
  59. * @return boolean TRUE if the given password matches the hashed password
  60. */
  61. public function validatePassword($password, $hashedPasswordAndSalt, $staticSalt = NULL) {
  62. return self::validateSaltedMd5($password, $hashedPasswordAndSalt);
  63. }
  64. }
  65. ?>