<?php
/**
* Hashes entries with a timeout for dry-run scenarios.
*
* @param array $entries An array of entries to hash. Each entry should be a string.
* @param string $hash_algorithm The hashing algorithm to use (e.g., 'md5', 'sha256').
* @param int $timeout Timeout in seconds for each hash operation.
* @return array An associative array where keys are the original entries and values are the hashes. Returns an empty array on error.
*/
function hashEntriesWithTimeout(array $entries, string $hash_algorithm, int $timeout): array
{
$results = [];
foreach ($entries as $entry) {
try {
if (strpos($hash_algorithm, 'sha') === 0) {
$hash = hash_raw($hash_algorithm, $entry, true); // Use hash_raw for SHA algorithms
} else {
$hash = hash($hash_algorithm, $entry);
}
$results[$entry] = $hash;
} catch (Exception $e) {
// Handle potential errors, e.g., invalid hash algorithm.
error_log("Error hashing entry '$entry': " . $e->getMessage());
$results[$entry] = null; // Or some other error indicator.
}
}
return $results;
}
// Example usage (dry-run scenario)
$entries = ["entry1", "entry2", "entry3"];
$hash_algorithm = "sha256";
$timeout = 5; // seconds
$hashed_entries = hashEntriesWithTimeout($entries, $hash_algorithm, $timeout);
print_r($hashed_entries);
?>
Add your comment