Reputation: 2005
I need to get md5 checksum of a file. But i don't have a file on a disk and i can not save it to disk. I have only a stream (socket) from where i can read a file.
How to calculate MD5 checksum in this case and get it to be same as i would calculate it for file on a disk?
I can read chunks of any size of a stream. Is it possible to get correct MD5 for this case?
I need some instructions of make md5sum from sums of chunks and get same result as
md5sum filetohash.txt
I code with Perl.
Upvotes: 2
Views: 422
Reputation: 126732
There is no need to collect the data into a complete file before calculating the checksum. You may add the data in pieces to a Digest::MD5
object, like this
my $md5 = Digest::MD5->new;
while ( my $chunk = read_stream() ) {
$md5->add($chunk);
}
print $md5->hexdigest, "\n";
Upvotes: 5