Lab 10: Security Hardening
Step 1: Secure Password Hashing — Argon2id
<?php
// Argon2id is the recommended algorithm (PHP 7.3+)
$hash = password_hash('MySecretPass123!', PASSWORD_ARGON2ID, [
'memory_cost' => 65536, // 64MB RAM
'time_cost' => 4, // 4 iterations
'threads' => 1, // Parallelism
]);
echo "Hash: " . substr($hash, 0, 30) . "...\n";
echo "Algorithm: " . password_get_info($hash)['algoName'] . "\n";
// Verify
echo "Correct password: " . (password_verify('MySecretPass123!', $hash) ? 'valid' : 'invalid') . "\n";
echo "Wrong password: " . (password_verify('wrong', $hash) ? 'valid' : 'invalid') . "\n";
// Check if rehash needed (after cost increase)
$needsRehash = password_needs_rehash($hash, PASSWORD_ARGON2ID, ['memory_cost' => 131072]);
echo "Needs rehash (higher cost): " . ($needsRehash ? 'yes' : 'no') . "\n";
// Compare bcrypt vs argon2id
echo "\nBcrypt hash: " . substr(password_hash('pass', PASSWORD_BCRYPT), 0, 30) . "...\n";
echo "Argon2id hash: " . substr(password_hash('pass', PASSWORD_ARGON2ID), 0, 30) . "...\n";Step 2: CSRF Token Protection
Step 3: Secure Random & Cryptographic Functions
Step 4: Session Security
Step 5: SQL Injection Prevention with PDO
Step 6: Content Security Policy & Security Headers
Step 7: Input Validation & Output Escaping
Step 8: Capstone — Secure Authentication Flow
Summary
Threat
Defense
PHP Function/Feature
Last updated
