rawurlencode((string) $k) . '=' . rawurlencode((string) $subIds[$k]), array_keys($subIds) )); $ts = (string) ($ts ?? time()); $nonce ??= bin2hex(random_bytes(16)); $sig = hash_hmac('sha256', implode("\n", ['krin-launch-v1', $placement, $userId, $ts, $nonce, $sub]), $secret); $query = ['placement' => $placement, 'user_id' => $userId, 'ts' => $ts, 'nonce' => $nonce, 'sig' => $sig]; foreach ($subIds as $k => $v) { $query["sub_ids[$k]"] = $v; } return 'https://wall.krin.io/w?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986); } // Verify a POST reward postback. $rawBody = file_get_contents('php://input'). function krin_verify_postback_post(string $rawBody, string $header, array $secrets, ?int $now = null): bool { $now ??= time(); $t = null; $sigs = []; foreach (explode(',', $header) as $part) { [$k, $v] = array_pad(explode('=', trim($part), 2), 2, ''); if ($k === 't') { $t = $v; } elseif ($k === 'v1') { $sigs[] = $v; } } if ($t === null || !ctype_digit($t) || abs($now - (int) $t) > 300) { return false; } foreach ($secrets as $secret) { $expected = hash_hmac('sha256', $t . '.' . $rawBody, $secret); foreach ($sigs as $v) { if (hash_equals($expected, $v)) { return true; } } } return false; } // Verify a GET reward postback (krin-pb-v2). $q = $_GET (already decoded). // A true result with $q['test'] === '1' is a test postback: never credit it. function krin_verify_postback_get(array $q, array $secrets, ?int $now = null): bool { $now ??= time(); $ts = (string) ($q['ts'] ?? ''); if (!ctype_digit($ts) || abs($now - (int) $ts) > 300) { return false; } $test = (string) ($q['test'] ?? ''); if ($test !== '0' && $test !== '1') { return false; } $message = implode("\n", ['krin-pb-v2', $ts, $q['conversion_id'] ?? '', $q['kind'] ?? '', $q['user_id'] ?? '', $q['reward'] ?? '', $q['payout_micros'] ?? '', $test]); foreach ($secrets as $secret) { if (hash_equals(hash_hmac('sha256', $message, $secret), (string) ($q['sig'] ?? ''))) { return true; } } return false; } // Advertiser S2S: sign the conversion you send to t.krin.io (postbacks.md 4.1, signature v2). // The signature covers the request exactly as sent: method, host, path and the query without sig. function krin_sign_inbound(string $secret, string $offerKey, string $clickId, string $event, string $txnId = '', ?int $ts = null): string { $host = 't.krin.io'; $path = '/pb/' . rawurlencode($offerKey); $params = ['click_id' => $clickId, 'event' => $event]; if ($txnId !== '') { $params['txn_id'] = $txnId; } $params['ts'] = (string) ($ts ?? time()); $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986); $sig = hash_hmac('sha256', implode("\n", ['krin-in-v2', 'GET', $host, $path, $query]), $secret); return "https://{$host}{$path}?{$query}&sig={$sig}"; }