浏览代码

Add : 检查字符串是否过期

Zhu Jinhui 7 年之前
父节点
当前提交
0e0418e888
共有 1 个文件被更改,包括 21 次插入2 次删除
  1. 21 2
      src/Library/Crypt.php

+ 21 - 2
src/Library/Crypt.php

@@ -109,6 +109,7 @@ class Crypt
     public function decryptWithTime($string)
     public function decryptWithTime($string)
     {
     {
         $passCrypt = $this->decrypt($string);
         $passCrypt = $this->decrypt($string);
+        if(!$passCrypt) return '';
 
 
         $expiredAt = substr($passCrypt, 0, 10);
         $expiredAt = substr($passCrypt, 0, 10);
 
 
@@ -117,6 +118,24 @@ class Crypt
         return substr($passCrypt, 10);
         return substr($passCrypt, 10);
     }
     }
 
 
+    /**
+     * 检查加密串是否已经过期
+     *
+     * @param string $string
+     * @return bool
+     */
+    public function checkDecryptExpired($string)
+    {
+        $passCrypt = $this->decrypt($string);
+        if(!$passCrypt) return false;
+
+        $expiredAt = substr($passCrypt, 0, 10);
+
+        if(time() > $expiredAt) return  false;
+
+        return true;
+    }
+
 	/**
 	/**
 	 * 解密字符
 	 * 解密字符
 	 *
 	 *
@@ -128,8 +147,8 @@ class Crypt
 		$string = str_replace(' ', '+', $string);
 		$string = str_replace(' ', '+', $string);
 		$string = base64_decode($this->verifyString($string));
 		$string = base64_decode($this->verifyString($string));
 
 
-		$passcrypt = openssl_decrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
-		return substr($passcrypt, 10);
+		$passCrypt = openssl_decrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
+		return substr($passCrypt, 10);
 	}
 	}
 
 
 	/**
 	/**