|  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
3 + 37 = ?
Subscribe to this entry?

 [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: Wed May 22 08:01:33 2024 UTC