auth('PSYCHO-XXXX-XXXX-XXXX-XXXX', $machineOrSessionId); * if (!$res['ok']) { die($res['code']); } */ final class PsychoKeyClient { private const HKDF_SALT = 'psychokey-salt-v1'; private string $secret; private string $token = ''; private string $hwid; /** @var array */ private array $vars = []; public function __construct( private string $app, string $apiSecretHex, private string $baseUrl = 'https://psycho-key.com', private string $version = '1.0.0', private bool $requireV2 = true, ?string $hwid = null ) { $bin = @hex2bin(trim($apiSecretHex)); if ($bin === false || strlen($bin) !== 32) { throw new InvalidArgumentException('API secret must be 64 hex chars (32 bytes).'); } $this->secret = $bin; $this->baseUrl = rtrim($baseUrl, '/'); $this->hwid = ($hwid !== null && preg_match('/^[A-Za-z0-9:_\-]{8,128}$/', $hwid)) ? $hwid : substr(hash('sha256', php_uname('n') . '|' . PHP_OS_FAMILY), 0, 32); } public function hwid(): string { return $this->hwid; } public function var(string $key, string $default = ''): string { return $this->vars[$key] ?? $default; } public function init(): array { return $this->call('init'); } public function auth(string $licenseKey, ?string $customHwid = null): array { if ($customHwid !== null && preg_match('/^[A-Za-z0-9:_\-]{8,128}$/', $customHwid)) { $this->hwid = $customHwid; } $r = $this->call('license', $licenseKey); if ($r['ok'] && $r['token'] !== '') { $this->token = $r['token']; } $this->vars = array_merge($this->vars, $r['variables']); return $r; } public function heartbeat(): array { $r = $this->call('heartbeat'); if ($r['ok'] && $r['token'] !== '') { $this->token = $r['token']; } $this->vars = array_merge($this->vars, $r['variables']); return $r; } public function logout(): array { $r = $this->call('logout'); $this->token = ''; return $r; } public function resetHwid(string $licenseKey): array { $r = $this->call('reset', $licenseKey); if ($r['ok'] && $r['token'] !== '') { $this->token = $r['token']; } $this->vars = array_merge($this->vars, $r['variables']); return $r; } private function call(string $action, string $licenseKey = ''): array { $out = [ 'ok' => false, 'code' => 'CLIENT_ERROR', 'message' => '', 'token' => '', 'retry_after' => 0, 'latest_version' => '', 'download_url' => '', 'variables' => [], 'license' => [], ]; $nonce = bin2hex(random_bytes(16)); $ts = time(); $canonical = implode('|', [$this->app, $action, $nonce, (string)$ts, $this->hwid, $licenseKey, $this->token]); $sig = hash_hmac('sha256', $canonical, $this->secret); $inner = json_encode([ 'action' => $action, 'nonce' => $nonce, 'ts' => $ts, 'hwid' => $this->hwid, 'license_key' => $licenseKey, 'token' => $this->token, 'version' => $this->version, 'sig' => $sig, ], JSON_UNESCAPED_SLASHES); $reqKey = hash_hkdf('sha256', $this->secret, 32, 'psychokey-req-v2|' . $this->app, self::HKDF_SALT); $iv = random_bytes(12); $tag = ''; $ct = openssl_encrypt((string)$inner, 'aes-256-gcm', $reqKey, OPENSSL_RAW_DATA, $iv, $tag, $this->app, 16); if ($ct === false) { $out['code'] = 'CLIENT_CRYPTO'; return $out; } $envelope = json_encode([ 'app' => $this->app, 'v' => 2, 'iv' => base64_encode($iv), 'ct' => base64_encode($ct), 'tag' => base64_encode($tag), ], JSON_UNESCAPED_SLASHES); $ch = curl_init($this->baseUrl . '/api/v1.php'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $envelope, CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'User-Agent: PsychoKey-PHP/' . $this->version], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, ]); $resp = curl_exec($ch); $err = curl_error($ch); curl_close($ch); if (!is_string($resp)) { $out['code'] = 'NETWORK'; $out['message'] = $err; return $out; } $root = json_decode($resp, true); if (!is_array($root)) { $out['code'] = 'CLIENT_PARSE'; return $out; } $encrypted = false; if (($root['v'] ?? 0) === 2) { $encrypted = true; $respKey = hash_hkdf('sha256', $this->secret, 32, 'psychokey-resp-v2|' . $this->app, self::HKDF_SALT); $rIv = base64_decode((string)($root['iv'] ?? ''), true); $rCt = base64_decode((string)($root['ct'] ?? ''), true); $rTag = base64_decode((string)($root['tag'] ?? ''), true); if ($rIv === false || $rCt === false || $rTag === false || strlen($rIv) !== 12 || strlen($rTag) !== 16) { $out['code'] = 'CLIENT_INTEGRITY'; return $out; } $aad = $this->app . '|' . $action . '|' . $nonce; $plain = openssl_decrypt($rCt, 'aes-256-gcm', $respKey, OPENSSL_RAW_DATA, $rIv, $rTag, $aad); if ($plain === false) { $out['code'] = 'CLIENT_INTEGRITY'; $out['message'] = 'Response authentication failed.'; return $out; } $payload = json_decode($plain, true); if (!is_array($payload)) { $out['code'] = 'CLIENT_PARSE'; return $out; } } else { if ($this->requireV2 && !empty($root['success'])) { $out['code'] = 'CLIENT_INTEGRITY'; $out['message'] = 'Unencrypted success rejected.'; return $out; } $payload = $root; } if ($encrypted && isset($payload['ts']) && abs((int)$payload['ts'] - $ts) > 300) { $out['code'] = 'CLIENT_INTEGRITY'; return $out; } $out['ok'] = (bool)($payload['success'] ?? false); $out['code'] = (string)($payload['code'] ?? ($out['ok'] ? 'OK' : 'ERROR')); $out['message'] = (string)($payload['message'] ?? ''); $out['retry_after'] = (int)($payload['retry_after'] ?? 0); $out['latest_version'] = (string)($payload['latest_version'] ?? ''); $out['download_url'] = (string)($payload['download_url'] ?? ''); if (is_array($payload['variables'] ?? null)) { foreach ($payload['variables'] as $k => $v) { if (is_scalar($v)) { $out['variables'][(string)$k] = (string)$v; } } } if ($out['ok']) { $out['token'] = (string)($payload['token'] ?? ''); if (is_array($payload['license'] ?? null)) { $out['license'] = $payload['license']; } } return $out; } }