<?php
/**
* Hashes log entries for a temporary solution with error logging.
*
* @param string $logEntry The log entry to hash.
* @param string $hashAlgorithm The hashing algorithm to use (e.g., 'sha256', 'md5'). Defaults to 'sha256'.
* @return string|false The hashed log entry, or false on error.
*/
function hashLogEntry(string $logEntry, string $hashAlgorithm = 'sha256'): string|false
{
try {
$hash = hash($hashAlgorithm, $logEntry); // Calculate the hash
return $hash; // Return the hashed value
} catch (Exception $e) {
error_log("Error hashing log entry: " . $e->getMessage()); // Log the error
return false; // Return false on error
}
}
/**
* Example usage.
*/
if (isset($_POST['logEntry'])) {
$logEntry = $_POST['logEntry'];
$hashAlgorithm = $_POST['hashAlgorithm'] ?? 'sha256'; // Default to sha256
$hashedValue = hashLogEntry($logEntry, $hashAlgorithm);
if ($hashedValue !== false) {
echo "Original Log Entry: " . htmlspecialchars($logEntry) . "<br>";
echo "Hashed Value (" . $hashAlgorithm . "): " . htmlspecialchars($hashedValue) . "<br>";
} else {
echo "Hashing failed. Check error logs.";
}
}
?>
Add your comment