1) { // q 是商 $q = intval($a / $m); $t = $m; // m 是余数,处理完后 a 和 m 互换 $m = $a % $m; $a = $t; // 更新 x0 和 x1 $t = $x0; $x0 = $x1 - $q * $x0; $x1 = $t; } // 确保 x1 是正数 if ($x1 < 0) $x1 += $m0; return $x1; } static function generateShortUniqueID($length = 8) { // 生成指定长度的随机字节,转为 Base64 编码并去除不必要字符 return substr(bin2hex(random_bytes($length / 2)), 0, $length); } static function idEncode($id = '') { if ($id <= 100234) { return $id . ''; } return id_encode($id); } static function idDecode($id = '') { $_id = intval($id); if ($_id == $id) { return $id; } return id_decode($id); } /** * 生成可逆的邀请码(8位,含校验位) * @param int $id 用户ID(需≥1000) * @return string 大写字母+数字组合 */ static function base62Encode(int $id, $secret = 'your_secret_salt'): string { // 添加校验位(防止篡改) $hash = crc32($id . $secret) % 1000; $code_num = $id * 1000 + $hash; // Base62 编码(0-9A-Za-z) $base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $code = ''; while ($code_num > 0) { $code = $base62[$code_num % 62] . $code; $code_num = (int) ($code_num / 62); } // 补全到8位 return str_pad($code, 8, '0', STR_PAD_LEFT); } /** * 从邀请码解析用户ID * @return int|false 成功返回id,失败返回false */ static function base62Decode(string $code, $secret = 'your_secret_salt'): int|false { $base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $code_num = 0; // Base62 解码 for ($i = 0; $i < strlen($code); $i++) { $pos = strpos($base62, $code[$i]); if ($pos === false) return false; $code_num = $code_num * 62 + $pos; } // 分离校验位 $id = (int) ($code_num / 1000); $hash = $code_num % 1000; // 校验 if (crc32($id . $secret) % 1000 != $hash) { return false; } return $id; } /** * Aes加密 * @param mixed $str * @param mixed $key * @return string */ static function aesencode($str, $key = '') { if (!$key) { $key = Config('pay.api_token'); } if (is_array($str) || is_object($str)) { $str = json_encode($str, JSON_UNESCAPED_UNICODE); } $key = hash('sha256', $key, true); $iv = substr($key, 0, 16); $encrypted = openssl_encrypt($str, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv); return base64_encode($encrypted); } /** * Aes解密 * @param mixed $str * @param mixed $key * @return string */ static function aesdecode($str, $key = '') { if (!$key) { $key = Config('pay.api_token'); } $key = hash('sha256', $key, true); $iv = substr($key, 0, 16); $encrypted = base64_decode($str); $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv); return $decrypted; } }