Skip to content

Instantly share code, notes, and snippets.

@rjindael
Last active February 23, 2021 14:32
Show Gist options
  • Save rjindael/b14d0b832c7ff301f02d9c31c4905aa3 to your computer and use it in GitHub Desktop.
Save rjindael/b14d0b832c7ff301f02d9c31c4905aa3 to your computer and use it in GitHub Desktop.
Example MD5 hash collision in PHP
<?php
// See: https://en.wikipedia.org/wiki/MD5#Collision_vulnerabilities
$x = "d131dd02c5e6eec4693d9a0698aff95c2fcab58712467eab4004583eb8fb7f8955ad340609f4b30283e488832571415a085125e8f7cdc99fd91dbdf280373c5bd8823e3156348f5bae6dacd436c919c6dd53e2b487da03fd02396306d248cda0e99f33420f577ee8ce54b67080a80d1ec69821bcb6a8839396f9652b6ff72a70";
$y = "d131dd02c5e6eec4693d9a0698aff95c2fcab50712467eab4004583eb8fb7f8955ad340609f4b30283e4888325f1415a085125e8f7cdc99fd91dbd7280373c5bd8823e3156348f5bae6dacd436c919c6dd53e23487da03fd02396306d248cda0e99f33420f577ee8ce54b67080280d1ec69821bcb6a8839396f965ab6ff72a70";
$x = hex2bin($x);
$y = hex2bin($y);
echo("MD5 Hash of X: " . md5($x) . "\n");
echo("MD5 Hash of Y: " . md5($y) . "\n");
echo("\n");
echo("Equals comparison: " . ($x == $y ? "TRUE" : "FALSE") . "\n");
echo("String comparison of MD5: " . (md5($x) == md5($y) ? "TRUE" : "FALSE") . "\n");
echo("String comparison of SHA256: " . (hash("sha256", $x) == hash("sha256", $y) ? "TRUE" : "FALSE") . "\n");
/*
* Expected output:
*
* MD5 Hash of X: 79054025255fb1a26e4bc422aef54eb4
* MD5 Hash of Y: 79054025255fb1a26e4bc422aef54eb4
*
* Equals comparison: FALSE
* String comparison of MD5: TRUE
* String comparison of SHA256: FALSE
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment