1. <?php
  2. /**
  3. * Hashes log entries for a temporary solution with error logging.
  4. *
  5. * @param string $logEntry The log entry to hash.
  6. * @param string $hashAlgorithm The hashing algorithm to use (e.g., 'sha256', 'md5'). Defaults to 'sha256'.
  7. * @return string|false The hashed log entry, or false on error.
  8. */
  9. function hashLogEntry(string $logEntry, string $hashAlgorithm = 'sha256'): string|false
  10. {
  11. try {
  12. $hash = hash($hashAlgorithm, $logEntry); // Calculate the hash
  13. return $hash; // Return the hashed value
  14. } catch (Exception $e) {
  15. error_log("Error hashing log entry: " . $e->getMessage()); // Log the error
  16. return false; // Return false on error
  17. }
  18. }
  19. /**
  20. * Example usage.
  21. */
  22. if (isset($_POST['logEntry'])) {
  23. $logEntry = $_POST['logEntry'];
  24. $hashAlgorithm = $_POST['hashAlgorithm'] ?? 'sha256'; // Default to sha256
  25. $hashedValue = hashLogEntry($logEntry, $hashAlgorithm);
  26. if ($hashedValue !== false) {
  27. echo "Original Log Entry: " . htmlspecialchars($logEntry) . "<br>";
  28. echo "Hashed Value (" . $hashAlgorithm . "): " . htmlspecialchars($hashedValue) . "<br>";
  29. } else {
  30. echo "Hashing failed. Check error logs.";
  31. }
  32. }
  33. ?>

Add your comment