Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php // Demonstrating CIA triad concepts in code // --- CONFIDENTIALITY: encrypt sensitive data at rest --- $plaintext = 'user credit card: 4111-1111-1111-1111'; $key = random_bytes(32); // 256-bit key $iv = random_bytes(16); // 128-bit IV for AES-CBC $ciphertext = openssl_encrypt($plaintext, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv); $encoded = base64_encode($iv . $ciphertext); // store IV with ciphertext echo "Encrypted: $encoded "; // --- INTEGRITY: hash-based message authentication code (HMAC) --- $message = json_encode(['user_id' => 42, 'action' => 'transfer', 'amount' => 500]); $secretKey = bin2hex(random_bytes(32)); $hmac = hash_hmac('sha256', $message, $secretKey); // Verify on receipt $incoming = $message; $expected = hash_hmac('sha256', $incoming, $secretKey); if (hash_equals($expected, $hmac)) { echo "Integrity check passed "; } else { echo "WARNING: message tampered "; } // --- AVAILABILITY: rate-limit login attempts --- function checkRateLimit(string $ip, int $maxAttempts = 5, int $windowSeconds = 300): bool { // Pseudo-code using a cache/Redis counter $key = "login_attempts:$ip"; $current = cache()->increment($key); if ($current === 1) { cache()->expire($key, $windowSeconds); } return $current <= $maxAttempts; }
Result
Open