|
@@ -0,0 +1,82 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace app\extra\dyLife;
|
|
|
|
|
+
|
|
|
|
|
+class SignUtil
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+ private $secret;
|
|
|
|
|
+ private $key;
|
|
|
|
|
+ private $iv;
|
|
|
|
|
+
|
|
|
|
|
+ public function init(string $secret = "") {
|
|
|
|
|
+ $this->secret = $secret;
|
|
|
|
|
+ $this->key = $this->parseSecret($secret);
|
|
|
|
|
+ $this->iv = substr($this->key, 16);
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @Description AES解密
|
|
|
|
|
+ * @param string $data base64后的密文
|
|
|
|
|
+ * @return string 明文
|
|
|
|
|
+ */
|
|
|
|
|
+ public function decryptAES($data) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $encrypted = $this->decode($data); // 先用base64解密
|
|
|
|
|
+ $decrypted = openssl_decrypt(
|
|
|
|
|
+ $encrypted,
|
|
|
|
|
+ 'AES-256-CBC',
|
|
|
|
|
+ $this->key,
|
|
|
|
|
+ OPENSSL_RAW_DATA,
|
|
|
|
|
+ $this->iv
|
|
|
|
|
+ );
|
|
|
|
|
+ return $decrypted;
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+// error_log($e->getMessage());
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * base64编码
|
|
|
|
|
+ */
|
|
|
|
|
+ public function encode($data) {
|
|
|
|
|
+ return base64_encode($data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * base64解码
|
|
|
|
|
+ */
|
|
|
|
|
+ public function decode($data) {
|
|
|
|
|
+ return base64_decode($data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function parseSecret($secret) {
|
|
|
|
|
+ $secret = $this->fillSecret($secret);
|
|
|
|
|
+ $secret = $this->cutSecret($secret);
|
|
|
|
|
+ return $secret;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function cutSecret($secret) {
|
|
|
|
|
+ if (strlen($secret) <= 32) {
|
|
|
|
|
+ return $secret;
|
|
|
|
|
+ }
|
|
|
|
|
+ $rightCnt = (int)((strlen($secret) - 32) / 2);
|
|
|
|
|
+ $leftCnt = strlen($secret) - 32 - $rightCnt;
|
|
|
|
|
+ return substr($secret, $leftCnt, 32);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function fillSecret($secret) {
|
|
|
|
|
+ if (strlen($secret) >= 32) {
|
|
|
|
|
+ return $secret;
|
|
|
|
|
+ }
|
|
|
|
|
+ $rightCnt = (int)((32 - strlen($secret)) / 2);
|
|
|
|
|
+ $leftCnt = 32 - strlen($secret) - $rightCnt;
|
|
|
|
|
+ $sb = str_repeat('#', $leftCnt);
|
|
|
|
|
+ $sb .= $secret;
|
|
|
|
|
+ $sb .= str_repeat('#', $rightCnt);
|
|
|
|
|
+ return $sb;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|