| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\extra\douyin;
- class SignUtil
- {
- /*
- * appid/client_key对应的client_secret TODO 这里换成服务商的appsecret
- */
- private static $secret = "12345678901234566543210987654321";
- private static $key;
- private static $iv;
- public static function init($secret) {
- self::$secret = $secret;
- self::$key = self::parseSecret(self::$secret);
- self::$iv = substr(self::$key, 16);
- }
- /**
- * AES解密
- * @param string $data base64后的密文
- * @return string 明文
- */
- public static function decryptAES($data) {
- try {
- $encrypted = self::decode($data); // 先用base64解密
- $decrypted = openssl_decrypt(
- $encrypted,
- 'AES-256-CBC',
- self::$key,
- OPENSSL_RAW_DATA,
- self::$iv
- );
- return $decrypted;
- } catch (\Exception $e) {
- error_log($e->getMessage());
- return null;
- }
- }
- /**
- * base64编码
- */
- public static function encode($data) {
- return base64_encode($data);
- }
- /**
- * base64解码
- */
- public static function decode($data) {
- return base64_decode($data);
- }
- private static function parseSecret($secret) {
- $secret = self::fillSecret($secret);
- $secret = self::cutSecret($secret);
- return $secret;
- }
- private static 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 static 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;
- }
- }
|