How can developers efficiently test for collisions in MD5 hashes in PHP?

To efficiently test for collisions in MD5 hashes in PHP, developers can generate multiple random strings, calculate their MD5 hashes, and store them in an array. Then, they can check for any duplicate hashes, which would indicate a collision. This approach allows developers to quickly identify if there are any collisions in a set of MD5 hashes.

<?php

$hashes = [];

// Generate multiple random strings
for ($i = 0; $i < 1000; $i++) {
    $randomString = bin2hex(random_bytes(16));
    $hash = md5($randomString);
    
    // Check for collisions
    if (in_array($hash, $hashes)) {
        echo "Collision found: $randomString\n";
        echo "Hash: $hash\n";
    }
    
    $hashes[] = $hash;
}

?>