1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| <?php class SecurePasswordManager { private const HASH_ALGORITHM = PASSWORD_ARGON2ID; private const HASH_OPTIONS = [ 'memory_cost' => 65536, 'time_cost' => 4, 'threads' => 3 ]; public function hashPassword(#[SensitiveParameter] string $password): string { return password_hash($password, self::HASH_ALGORITHM, self::HASH_OPTIONS); } public function verifyPassword( #[SensitiveParameter] string $password, string $hash ): bool { return password_verify($password, $hash); } public function needsRehash(string $hash): bool { return password_needs_rehash($hash, self::HASH_ALGORITHM, self::HASH_OPTIONS); } public function getHashInfo(string $hash): array { return password_get_info($hash); } public function generateSecurePassword(int $length = 16): string { $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*'; $password = ''; for ($i = 0; $i < $length; $i++) { $password .= $characters[random_int(0, strlen($characters) - 1)]; } return $password; } public function assessPasswordStrength(#[SensitiveParameter] string $password): array { $score = 0; $feedback = []; if (strlen($password) >= 8) { $score += 1; } else { $feedback[] = '密码长度至少需要8个字符'; } if (strlen($password) >= 12) { $score += 1; } if (preg_match('/[a-z]/', $password)) { $score += 1; } else { $feedback[] = '需要包含小写字母'; } if (preg_match('/[A-Z]/', $password)) { $score += 1; } else { $feedback[] = '需要包含大写字母'; } if (preg_match('/[0-9]/', $password)) { $score += 1; } else { $feedback[] = '需要包含数字'; } if (preg_match('/[^A-Za-z0-9]/', $password)) { $score += 1; } else { $feedback[] = '需要包含特殊字符'; } if (!preg_match('/(.)\1{2,}/', $password)) { $score += 1; } else { $feedback[] = '避免连续重复字符'; } $strength = match(true) { $score >= 6 => 'strong', $score >= 4 => 'medium', $score >= 2 => 'weak', default => 'very_weak' }; return [ 'score' => $score, 'max_score' => 7, 'strength' => $strength, 'feedback' => $feedback ]; } }
class EncryptionManager { private const CIPHER = 'aes-256-gcm'; private string $key; public function __construct(#[SensitiveParameter] string $key) { if (strlen($key) !== 32) { throw new InvalidArgumentException('密钥长度必须为32字节'); } $this->key = $key; } public function encrypt(#[SensitiveParameter] string $data): array { $iv = random_bytes(12); $tag = ''; $encrypted = openssl_encrypt( $data, self::CIPHER, $this->key, OPENSSL_RAW_DATA, $iv, $tag ); if ($encrypted === false) { throw new RuntimeException('加密失败'); } return [ 'data' => base64_encode($encrypted), 'iv' => base64_encode($iv), 'tag' => base64_encode($tag) ]; } public function decrypt(array $encryptedData): string { $data = base64_decode($encryptedData['data']); $iv = base64_decode($encryptedData['iv']); $tag = base64_decode($encryptedData['tag']); $decrypted = openssl_decrypt( $data, self::CIPHER, $this->key, OPENSSL_RAW_DATA, $iv, $tag ); if ($decrypted === false) { throw new RuntimeException('解密失败'); } return $decrypted; } public static function generateKey(): string { return random_bytes(32); } public static function deriveKey( #[SensitiveParameter] string $password, string $salt, int $iterations = 100000 ): string { return hash_pbkdf2('sha256', $password, $salt, $iterations, 32, true); } }
echo "\n=== 密码管理示例 ===\n";
$passwordManager = new SecurePasswordManager();
$generatedPassword = $passwordManager->generateSecurePassword(12); echo "生成的安全密码: $generatedPassword\n";
$testPasswords = ['123456', 'password', 'MySecure123!', 'VeryComplexP@ssw0rd2023'];
foreach ($testPasswords as $password) { $strength = $passwordManager->assessPasswordStrength($password); echo "密码强度评估 - 强度: {$strength['strength']}, 得分: {$strength['score']}/{$strength['max_score']}\n"; if (!empty($strength['feedback'])) { echo " 建议: " . implode(', ', $strength['feedback']) . "\n"; } }
$password = 'MySecurePassword123!'; $hash = $passwordManager->hashPassword($password); echo "\n密码哈希: " . substr($hash, 0, 50) . "...\n";
$isValid = $passwordManager->verifyPassword($password, $hash); echo "密码验证: " . ($isValid ? '通过' : '失败') . "\n";
$hashInfo = $passwordManager->getHashInfo($hash); echo "哈希信息: " . json_encode($hashInfo) . "\n";
echo "\n=== 加密示例 ===\n";
$encryptionKey = EncryptionManager::generateKey(); $encryption = new EncryptionManager($encryptionKey);
$sensitiveData = '这是需要加密的敏感信息'; $encrypted = $encryption->encrypt($sensitiveData);
echo "加密数据: " . substr($encrypted['data'], 0, 30) . "...\n"; echo "IV: " . $encrypted['iv'] . "\n"; echo "Tag: " . $encrypted['tag'] . "\n";
$decrypted = $encryption->decrypt($encrypted); echo "解密数据: $decrypted\n";
$userPassword = 'user_password_123'; $salt = random_bytes(16); $derivedKey = EncryptionManager::deriveKey($userPassword, $salt);
echo "\n密钥派生:\n"; echo "盐值: " . bin2hex($salt) . "\n"; echo "派生密钥: " . bin2hex($derivedKey) . "\n"; ?>
|