Hash.php 802 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace mall\utils;
  3. class Hash {
  4. public static function encrypt($name, $value, $salt = '!kQm*fF3pXe1Kbm%9') {
  5. if(empty($value)){
  6. return false;
  7. }
  8. $md5 = md5($salt.$name.$value);
  9. $string = implode("|", [$md5,$name,$value]);
  10. return str_replace("=", "", base64_encode($string));
  11. }
  12. public static function decrypt($data, $salt = '!kQm*fF3pXe1Kbm%9') {
  13. $string = base64_decode($data);
  14. $arr = explode('|', $string);
  15. if (count($arr) != 3) {
  16. return false;
  17. }
  18. list($md5,$name,$value) = $arr;
  19. $new_md5 = md5($salt.$name.$value);
  20. if($new_md5 == $md5){
  21. return ["name"=>$name,"value"=>$value];
  22. }
  23. return false;
  24. }
  25. }