During file transfer or downloading, especially large files, interruptions are often encountered. To ensure file integrity, files are usually hashed and verified during file transfer. PHP provides powerful hashing function, and the hash_update_stream function is one of them. It can be used to handle hash calculations of large files, especially suitable for verification of breakpoint continuous files.
This article will introduce how to use hash_update_stream to implement hash integrity verification of files to ensure that files can be integrity verification when they continue to download after interruption.
The hash_update_stream function is a function in PHP that calculates hash values during the process of reading stream data. Unlike hash_update , the latter requires the entire data to be loaded into memory at one time, while hash_update_stream can gradually read the file stream for hash calculations, so it is especially suitable for large files or files that need to be processed in chunks.
bool hash_update_stream ( resource $context , resource $stream [, int $length ] )
$context : hash resource, usually created by hash_init .
$stream : File stream resource, usually obtained through fopen() .
$length : The number of bytes to be read, the default is 8192 bytes.
During file transfer, if the transmission is disconnected midway, the breakpoint transfer mechanism will continue to download from the disconnected position. In order to verify the file integrity after the breakpoint is continuously transmitted, the hash value of the file is usually calculated on the server side and compared with the hash value transmitted by the client.
Suppose you have a breakpoint to continue downloading the file bigfile.zip , you need to verify that the file has been tampered with or lost some of the content during the transmission process. The following are the steps to implement hash integrity verification.
Suppose we need to download a file bigfile.zip and verify the integrity of the file when it is being transferred to the breakpoint. The following code shows how to calculate the hash of a file using the hash_update_stream function during breakpoint continuation.
<?php
// Initial hash context(use SHA256 algorithm)
$hashContext = hash_init('sha256');
// Open the file stream
$fileStream = fopen('https://m66.net/bigfile.zip', 'rb');
if (!$fileStream) {
die("Unable to open the file!\n");
}
// Calculate the hash value of the file
while (!feof($fileStream)) {
// Read the file and update the hash
$data = fread($fileStream, 8192);
hash_update_stream($hashContext, $data);
}
// Get the final hash value
$fileHash = hash_final($hashContext);
// The hash value of the output file
echo "FiledSHA-256Hash value:$fileHash\n";
// Close the file stream
fclose($fileStream);
// 假设已经在服务器端生成并存储了Filed正确Hash value
$serverStoredHash = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
// 比较本地计算的Hash value与服务器存储的Hash value
if ($fileHash === $serverStoredHash) {
echo "File integrity verification was successful!\n";
} else {
echo "File verification failed,The file may have been corrupted or tampered!\n";
}
?>
hash_init : Initialize a hash context and select the sha256 algorithm to generate the hash value.
fopen : Open the file stream to be downloaded, here we use https://m66.net/bigfile.zip as the sample file address.
hash_update_stream : gradually reads each piece of data in the file and updates the hash value. The data block size read per time is 8192 bytes.
hash_final : Gets the final hash value of the file.
File Verification : Comparison of the calculated hash value with the correct hash value stored on the server to verify the integrity of the file.
In a scenario where breakpoints continue, we usually store a "downloaded part" hash value and then calculate the hash of the file part starting at the last breakpoint as we continue to download. To do this, fread in the code can be used with fseek to control the read position of the file, thereby ensuring that the calculation starts from the specified position.
// Set the file flow pointer to the specified location(For example, the part where the breakpoint is continued)
fseek($fileStream, $resumePosition);
As we continue to download, we continue to read the data blocks from the resumePosition location and update the hash, finally verifying the integrity of the entire file.
By using hash_update_stream , PHP provides an efficient way to calculate the hash value of large files, especially when transferring files with breakpoint continuous transmission. In this way, we can ensure the integrity and correctness of the file during the transfer process, and avoid data loss or corruption after the file is disconnected in the middle.
If you are developing a file download or upload system that needs to support breakpoint continuous transmission, combined with hash_update_stream function for hash verification, it will provide you with a reliable data integrity guarantee.