<?php 
    require_once ('.hta_config/db_config.php'); 
?>
<section class="diZContainer diZmxAuto diZmy8">
    <h2 class="diZBorderBottom">MD5 Decryption</h2>
    <p class="diZTextJustify">The MD5 Decryption Tool is designed to help you decrypt MD5 hashes. It searches a pre-existing database for the original text corresponding to a given MD5 hash and retrieves it if available. This tool is especially useful for developers and security professionals who need to quickly look up the original value of an MD5 hash.</p>
    <div class="diZMaxW600 diZmxAuto toolsSection diZmy20">
        <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                try {
                    $db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
                    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $stmt = $db->prepare("SELECT * FROM md5_data WHERE md5 = :md5");
                    $stmt->bindParam(':md5', $_POST["md5-hash"]);
                    $stmt->execute();
                    $result = $stmt->fetch(PDO::FETCH_ASSOC);
                    
                    if ($result) {
                        echo '  <div class="">
                                    <p>Given MD5 Vlue: </p>
                                    <p class="diZDisplayNone" id="copied-notice"></p>
                                    <div class="diZFlexColumn">
                                    <textarea class="diZTextAreaResizeNone" rows="8" id="output" readonly>'.$result['value'].'</textarea><br>
                                    <button class="diZmxAuto" id="copyButton" onclick="copyOutputText()"><span>Copy</span></button>
                                    </div>
                                </div>';
                            echo '
                                <script>
                                    function copyOutputText() {
                                        const outputText = document.getElementById(\'output\').value;
                                        const textarea = document.createElement(\'textarea\');
                                        textarea.value = outputText;
                                        document.body.appendChild(textarea);
                                        textarea.select();
                                        document.execCommand(\'copy\');
                                        document.body.removeChild(textarea);
                                        document.getElementById(\'copied-notice\').style.display = \'block\';
                                        document.getElementById(\'copied-notice\').innerHTML = \'MD5 value copied to clipboard!\';
                                    }
                                </script>';
                    } else {
                        echo '<div>
                                <div>
                                    <p>No data found for the given MD5 hash </p>
                                    <div>
                                        <button class="" id="" onclick="tryAgain()">Try Again</button>
                                    </div>
                                </div>
                            </div>';
                            echo '  <script>
                                        function tryAgain() {
                                            window.location.href="/tools/md5-decryption";
                                        }
                                    </script>' ;
                        // echo json_encode(array('success' => false, 'message' => 'No data found for the given MD5 hash'));
                    }
                } catch (PDOException $e) {
                    echo json_encode(array('success' => false, 'message' => 'Database error: ' . $e->getMessage()));
                    exit();
                }
            }else{
                echo '  <form method="post" action="" class="diZFlexColumn">
                            <label for="md5-hash" class="custom-label">Enter MD5 Hash:</label><br>
                            <textarea id="md5-hash" name="md5-hash" class="diZTextAreaResizeNone" rows="8"></textarea><br>
                            <button class="diZmxAuto" type="submit"><span>Convert MD5</span></button>
                        </form>';
            }
        ?> 
    </div>
    <div>
        <h3>Usage:</h3>
        <ul>
            <li><strong>Enter MD5 Hash:</strong> Input the MD5 hash you want to decrypt into the provided text area.</li>
            <li><strong>Decrypt MD5:</strong> Click the "Convert MD5" button to initiate the search for the corresponding value in the database.</li>
            <li><strong>View and Copy Result:</strong> If a matching value is found, it will be displayed in the output area. Click the "Copy" button to copy the result to your clipboard.</li>
            <li><strong>Try Again:</strong> If no matching value is found, click the "Try Again" button to search for another MD5 hash.</li>
        </ul>
        
        <h3>Features:</h3>
        <ul>
            <li>Simple and user-friendly interface for easy MD5 hash decryption.</li>
            <li>Instantly displays the original text if found in the database.</li>
            <li>Copy functionality to quickly copy the decrypted text to your clipboard.</li>
            <li>Notification message to indicate successful copying of the decrypted text.</li>
        </ul>
        
        <h3>Example Use Cases:</h3>
        <ul>
            <li>Developers verifying the integrity of files by comparing original text and its MD5 hash.</li>
            <li>Security professionals analyzing hashed data to check for common vulnerabilities.</li>
            <li>Researchers decrypting MD5 hashes for data analysis purposes.</li>
        </ul>
    </div>
</section>
