<?php
/**
 * Password Generator Tool
 * URL: https://swabhimanmfi.com/generate_password.php
 */
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Generator - Swabhiman MFI</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .card {
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 20px 20px 0 0 !important;
            padding: 20px;
        }
        .btn-generate {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            padding: 12px 30px;
            border-radius: 10px;
            color: white;
            font-weight: 600;
        }
        .btn-generate:hover {
            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
            color: white;
        }
        .result-box {
            background: #f8f9fc;
            border-radius: 10px;
            padding: 20px;
            margin-top: 20px;
        }
        .encrypted-value {
            font-size: 24px;
            font-weight: 700;
            color: #667eea;
        }
        .copy-btn {
            cursor: pointer;
            color: #667eea;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header text-center">
                        <h4 class="mb-0">Password Generator</h4>
                        <small>Generate encrypted password for database</small>
                    </div>
                    <div class="card-body p-4">
                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label"><strong>Enter Password</strong></label>
                                <input type="text" name="password" id="password" class="form-control form-control-lg"
                                       placeholder="Enter password (e.g., 123456)"
                                       value="<?= isset($_POST['password']) ? htmlspecialchars($_POST['password']) : ''; ?>" required>
                            </div>
                            <button type="submit" name="generate" class="btn btn-generate w-100">
                                Generate Encrypted Password
                            </button>
                        </form>

                        <?php if(isset($_POST['generate']) && !empty($_POST['password'])): ?>
                            <?php
                            $plain_password = $_POST['password'];
                            $md5_hash = md5($plain_password);
                            $crc32_value = crc32($md5_hash);
                            $encrypted_password = abs($crc32_value);
                            ?>
                            <div class="result-box">
                                <h5 class="text-success mb-3">Password Generated Successfully!</h5>

                                <table class="table table-bordered">
                                    <tr>
                                        <td><strong>Plain Password:</strong></td>
                                        <td><?= htmlspecialchars($plain_password); ?></td>
                                    </tr>
                                    <tr>
                                        <td><strong>MD5 Hash:</strong></td>
                                        <td><code><?= $md5_hash; ?></code></td>
                                    </tr>
                                    <tr>
                                        <td><strong>CRC32 of MD5:</strong></td>
                                        <td><code><?= $crc32_value; ?></code></td>
                                    </tr>
                                    <tr class="table-success">
                                        <td><strong>Final Encrypted Password:</strong></td>
                                        <td>
                                            <span class="encrypted-value" id="encrypted"><?= $encrypted_password; ?></span>
                                            <button class="btn btn-sm btn-outline-primary ms-2" onclick="copyToClipboard()">Copy</button>
                                        </td>
                                    </tr>
                                </table>

                                <div class="alert alert-info mt-3">
                                    <strong>SQL Query to Update Password:</strong>
                                    <pre class="mb-0 mt-2">UPDATE users SET password = '<?= $encrypted_password; ?>' WHERE email = 'user@example.com';</pre>
                                </div>

                                <div class="alert alert-warning mt-3">
                                    <strong>Formula Used:</strong><br>
                                    <code>abs(crc32(md5('<?= htmlspecialchars($plain_password); ?>')))</code> = <code><?= $encrypted_password; ?></code>
                                </div>
                            </div>
                        <?php endif; ?>

                        <hr class="my-4">

                        <h5>Common Passwords Reference:</h5>
                        <table class="table table-sm table-striped">
                            <thead>
                                <tr>
                                    <th>Password</th>
                                    <th>Encrypted Value</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $common_passwords = ['123456', '12345678', 'password', 'admin', 'user123', '111111', '000000'];
                                foreach($common_passwords as $pwd) {
                                    $enc = abs(crc32(md5($pwd)));
                                    echo "<tr><td>{$pwd}</td><td><code>{$enc}</code></td></tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
    function copyToClipboard() {
        var text = document.getElementById('encrypted').innerText;
        navigator.clipboard.writeText(text).then(function() {
            alert('Copied: ' + text);
        });
    }
    </script>
</body>
</html>
