$value) { if ($value === null || $value === '') { continue; } $filtered[(string)$key] = (string)$value; } ksort($filtered, SORT_STRING); $parts = []; foreach ($filtered as $key => $value) { $parts[] = $key . '=' . $value; } return implode('&', $parts); } /** * 校验飞鹅回调签名 */ public static function verify(array $params) { $sign = trim((string)($params['sign'] ?? '')); if ($sign === '') { return false; } $publicKeyPath = self::getPublicKeyPath(); if (!is_file($publicKeyPath)) { Yii::error('飞鹅公钥文件不存在: ' . $publicKeyPath, __METHOD__); return false; } $publicKey = file_get_contents($publicKeyPath); if ($publicKey === false || trim($publicKey) === '') { Yii::error('飞鹅公钥文件读取失败: ' . $publicKeyPath, __METHOD__); return false; } $signContent = self::buildSignContent($params); $signature = base64_decode($sign, true); if ($signature === false) { Yii::warning('飞鹅回调 sign Base64 解码失败', __METHOD__); return false; } $key = openssl_pkey_get_public($publicKey); if ($key === false) { Yii::error('飞鹅公钥解析失败: ' . openssl_error_string(), __METHOD__); return false; } $result = openssl_verify($signContent, $signature, $key, OPENSSL_ALGO_SHA256); if (PHP_VERSION_ID < 80000) { openssl_free_key($key); } return $result === 1; } }