|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54490 Add function md5_stream
Submitted: 2011-04-08 03:28 UTC Modified: 2011-04-08 09:52 UTC
From: mtdowling at gmail dot com Assigned:
Status: Wont fix Package: hash related
PHP Version: 5.3.6 OS: Mac OS X
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: mtdowling at gmail dot com
New email:
PHP Version: OS:


 [2011-04-08 03:28 UTC] mtdowling at gmail dot com
When calculating a MD5 hash using the contents of a PHP stream, we currently need to load the entire stream contents into a string then use the md5() function.  Loading the contents of a stream into a string can potentially use a large amount of memory.

md5_file() is great for working with registered stream wrappers, but does not allow developers to use an already open stream.  md5_file will open a stream, read 1,024 byte chunks to calculate the hash, and then close the stream.

I think it would be beneficial to add a md5_stream() function.  The md5_stream() function would be identical to md5_file(), but it would calculate the MD5 hash using the contents of an already opened PHP stream.

The attached test script shows the current memory drawbacks of calculating the md5 hash using the contents of a large stream.

Test script:

$f = fopen('php://temp/maxmemory:32', 'r+');
$filler = implode('', range('A', 'Z'));
for ($i = 0; $i < 10000; $i++) {
    fwrite($f, $filler);

fseek($f, 0);
$contents = stream_get_contents($f);
$hash = md5($contents);

echo memory_get_peak_usage();


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 09:52 UTC]
-Status: Open +Status: Wont fix
 [2011-04-08 09:52 UTC]
You can already do this with hash_update_stream() (in conjunction with hash_init() and hash_final(), of course). I don't think there's any need for an algorithm-specific function here.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 15:01:31 2024 UTC