Hash.php 1.1 KB

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