Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] logowanie
Forum PHP.pl > Forum > Przedszkole
damain1960
czy ktoś mógł by usunąć "hash" i zamienić go na "md5" z tego kodu
  1. <?php
  2.  
  3. class Password
  4. {
  5. // these constants may be changed without breaking existing hashes.
  6. const PBKDF2_HASH_ALGORITHM = 'sha256';
  7. const PBKDF2_ITERATIONS = 1000;
  8. const PBKDF2_SALT_BYTE_SIZE = 24;
  9. const PBKDF2_HASH_BYTE_SIZE = 24;
  10. const HASH_SECTIONS = 4;
  11. const HASH_ALGORITHM_INDEX = 0;
  12. const HASH_ITERATION_INDEX = 1;
  13. const HASH_SALT_INDEX = 2;
  14. const HASH_PBKDF2_INDEX = 3;
  15.  
  16. static function createHash($password)
  17. {
  18. // format: algorithm:iterations:salt:hash
  19. $salt = base64_encode(mcrypt_create_iv(self::PBKDF2_SALT_BYTE_SIZE, MCRYPT_DEV_URANDOM));
  20. return self::PBKDF2_HASH_ALGORITHM . ":" . self::PBKDF2_ITERATIONS . ":" . $salt . ":" .
  21. base64_encode(self::pbkdf2(
  22. self::PBKDF2_HASH_ALGORITHM, $password, $salt, self::PBKDF2_ITERATIONS, self::PBKDF2_HASH_BYTE_SIZE, true
  23. ));
  24. }
  25.  
  26. static function validatePassword($password, $correct_hash)
  27. {
  28. $params = explode(":", $correct_hash);
  29. if (count($params) < self::HASH_SECTIONS)
  30. return false;
  31. $pbkdf2 = base64_decode($params[self::HASH_PBKDF2_INDEX]);
  32. return self::slowEquals(
  33. $pbkdf2, self::pbkdf2(
  34. $params[self::HASH_ALGORITHM_INDEX], $password, $params[self::HASH_SALT_INDEX], (int) $params[self::HASH_ITERATION_INDEX], strlen($pbkdf2), true
  35. )
  36. );
  37. }
  38.  
  39. // compares two strings $a and $b in length-constant time.
  40. static function slowEquals($a, $b)
  41. {
  42. $diff = strlen($a) ^ strlen($b);
  43. for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
  44. {
  45. $diff |= ord($a[$i]) ^ ord($b[$i]);
  46. }
  47. return $diff === 0;
  48. }
  49.  
  50. static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
  51. {
  52. $algorithm = strtolower($algorithm);
  53. if (!in_array($algorithm, hash_algos(), true))
  54. trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR);
  55. if ($count <= 0 || $key_length <= 0)
  56. trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR);
  57.  
  58. if (function_exists("hash_pbkdf2"))
  59. {
  60. // The output length is in NIBBLES (4-bits) if $raw_output is false!
  61. if (!$raw_output)
  62. {
  63. $key_length = $key_length * 2;
  64. }
  65. return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output);
  66. }
  67.  
  68. $hash_length = strlen(hash($algorithm, "", true));
  69. $block_count = ceil($key_length / $hash_length);
  70.  
  71. $output = "";
  72. for ($i = 1; $i <= $block_count; $i++)
  73. {
  74. // $i encoded as 4 bytes, big endian.
  75. $last = $salt . pack("N", $i);
  76. // first iteration
  77. $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
  78. // perform the other $count - 1 iterations
  79. for ($j = 1; $j < $count; $j++)
  80. {
  81. $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
  82. }
  83. $output .= $xorsum;
  84. }
  85.  
  86. if ($raw_output)
  87. return substr($output, 0, $key_length);
  88. else
  89. return bin2hex(substr($output, 0, $key_length));
  90. }
  91.  
  92. }


pomoże ktoś?

proszę niech ktoś mi pomoże.
viking
Po co chcesz zmieniać jakiś tam algorytm szyfrujący na funkcję hashującą?

  1. public static function validatePassword($pass1, $pass2) {
  2. return md5($pass1)===md5($pass2);
  3. }
com
Zapomnij o md5 wink.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.