<?php
/**
* Hashes log stream values for batch processing.
*
* @param string $logStreamPath Path to the log file.
* @param string $hashAlgorithm The hashing algorithm to use (e.g., 'sha256').
* @param string $outputFile Path to the output file for hashed values.
* @return void
*/
function hashLogStream(string $logStreamPath, string $hashAlgorithm, string $outputFile): void
{
try {
// Open the log file for reading.
$logFile = fopen($logStreamPath, 'r');
if (!$logFile) {
error_log("Error: Could not open log file: $logStreamPath");
return;
}
// Open the output file for writing.
$outputFileHandle = fopen($outputFile, 'w');
if (!$outputFileHandle) {
error_log("Error: Could not open output file: $outputFile");
fclose($logFile);
return;
}
// Process each line in the log file.
while (($line = fgets($logFile)) !== false) {
// Skip empty lines.
if (empty($line)) {
continue;
}
// Hash the log line.
$hash = hash($hashAlgorithm, $line);
// Write the original line and its hash to the output file.
fprintf($outputFileHandle, "%s|%s\n", $line, $hash);
}
// Close the log file and the output file.
fclose($logFile);
fclose($outputFileHandle);
echo "Log stream hashed successfully. Hashed values written to: $outputFile\n";
} catch (Exception $e) {
error_log("An unexpected error occurred: " . $e->getMessage());
}
}
// Example usage:
// hashLogStream('/path/to/your/log.txt', 'sha256', '/path/to/hashed_logs.txt');
?>
Add your comment