[PEAR-BUG] Bug #20350 [Opn->Csd]: Duplicate md5 checksum using uniqid()

[email protected]
Newsgroups php.pear.bugs
Message-ID <[email protected]>
Edit report at https://pear.php.net/bugs/bug.php?id=20350&edit=1

 ID:               20350
 Updated by:       [email protected]
 Reported By:      werner at 21digital dot co dot za
 Summary:          Duplicate md5 checksum using uniqid()
-Status:           Open
+Status:           Closed
 Type:             Bug
-Package:          Mail
+Package:          Mail_Mime
 Operating System: Windows 7
 Package Version:  1.2.0
 PHP Version:      5.5.15
-Assigned To:      
+Assigned To:      alec
 Roadmap Versions: 
 New Comment:

-Status:      Open
+Status:      Closed
-Package:     Mail
+Package:     Mail_Mime
-Assigned To:
+Assigned To: alec
This is fixed in recent Mail-Mime versions.


Previous Comments:
------------------------------------------------------------------------

[2014-07-25 11:20:43] #337ce55d171b74b61ca

Description:
------------
We where on 5.2.16 and recently upgrade to 5.3.28 when the Mail and
Mail_Mime class to misbehave.

On mail.php, line 300:
'cid'    => md5(uniqid(time()))

On mimePart.php, line 245:
srand((double)microtime()*1000000);
$boundary = '=_' . md5(rand() . microtime());

In the above test, on the new version of PHP, the CID get duplicated in
the encode() function breaking mail clients such as ThunderBird. 

Solution:

Change both lines to:
On mail.php, line 300:
'cid'    => md5(uniqid("",true))

On mimePart.php, line 243:
$boundary = '=_' . md5(uniqid("", true));
(remove both lines)
srand((double)microtime()*1000000);
$boundary = '=_' . md5(rand() . microtime());

This ensures that the cid is always unique. I tested the solution in all
three version of php and works like a charm.

Test script:
---------------
<?php
$sums = array();
for ($i = 0; $i < 100; $i++) {
   $sums[] = md5(uniqid("",true));
}
print_r($sums);
?>

Expected result:
----------------
[0] => 9456f6c830eab93f3bed29d3a6de7cbc
[1] => 8541b5441ca5c5891be1cf2505d928f4
[2] => ca5989c75b3f4b7dccd0b43860a6fce7
[3] => 5aad6a5bd07fcd4ac636a493a1aab437
[4] => c6e74df96979ecadd4a91cb5b0860f03
[5] => 86be925e498e2b6cef44b3f869cc6d15
[6] => 1bef1f71656613505e673bc6d128ad88
[7] => ec50f5e30b31c9f80d6efd15691921ff
[8] => 626e67333ec14e435e0d7b282696d289
[9] => 55b2a57a51095022f80fd7c3a5d24976
[10] => 0a67cdbb6185e7d1420fc4e498a2da75
[11] => ca2b86328eef1682ccf4cc2fe7d219bb
[12] => 2deb8ed0c5f30e202fb79844e09abd62
[13] => 543ceaf778b3e88bcda0e4c6f2cc39d8
[14] => 16a5afc33d62a946d0930686184c4962

Actual result:
--------------
Note the duplicates in PHP 5.3.28 Stable nts and 5.5.15 V11 Stable nts

[0] => b73adca2f80fe764515eaba523550d3c
[1] => b73adca2f80fe764515eaba523550d3c
[2] => b73adca2f80fe764515eaba523550d3c
[3] => b73adca2f80fe764515eaba523550d3c
[4] => 2ec091b61fe9f2caf40ce4de9b563085
[5] => 2ec091b61fe9f2caf40ce4de9b563085
[6] => 2ec091b61fe9f2caf40ce4de9b563085
[7] => 2ec091b61fe9f2caf40ce4de9b563085
[8] => 46fd8b8fe1adb741cd151d630f093213
[9] => 46fd8b8fe1adb741cd151d630f093213
[10] => 46fd8b8fe1adb741cd151d630f093213
[11] => 46fd8b8fe1adb741cd151d630f093213
[12] => 46fd8b8fe1adb741cd151d630f093213
[13] => 46fd8b8fe1adb741cd151d630f093213
[14] => dfc9d2239ba8dd86b8d845dc4158ae77

------------------------------------------------------------------------


-- 
Edit this bug report at https://pear.php.net/bugs/bug.php?id=20350&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.