Hash.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Utils;
  3. use App\Services\Config;
  4. class Hash
  5. {
  6. /**
  7. * @param $str
  8. * @return string
  9. */
  10. public static function passwordHash($str)
  11. {
  12. $method = Config::get('pwdMethod');
  13. switch ($method) {
  14. case 'md5':
  15. return self::md5WithSalt($str);
  16. break;
  17. case 'sha256':
  18. return self::sha256WithSalt($str);
  19. break;
  20. default:
  21. return self::md5WithSalt($str);
  22. }
  23. return $str;
  24. }
  25. public static function cookieHash($str)
  26. {
  27. return substr(hash('sha256', $str . Config::get('key')), 5, 45);
  28. }
  29. /**
  30. * @param $pwd
  31. * @return string
  32. */
  33. public static function md5WithSalt($pwd)
  34. {
  35. $salt = Config::get('salt');
  36. return md5($pwd . $salt);
  37. }
  38. /**
  39. * @param $pwd
  40. * @return string
  41. */
  42. public static function sha256WithSalt($pwd)
  43. {
  44. $salt = Config::get('salt');
  45. return hash('sha256', $pwd . $salt);
  46. }
  47. // @TODO
  48. public static function checkPassword($hashedPassword, $password)
  49. {
  50. $method = Config::get('pwdMethod');
  51. if ($hashedPassword == self::passwordHash($password)) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. }