Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created July 29, 2010 21:11
Show Gist options
  • Save Mikulas/499244 to your computer and use it in GitHub Desktop.
Save Mikulas/499244 to your computer and use it in GitHub Desktop.
Experimental check for hash cycles
<?php
/**
* Experimental check for hash cycles
* Should take about 16 hours, 20 minutes to compute hashes and few more minutes to compare them
* @author Mikulas Dite
* @copyright Mikulas Dite 2010
* @version MD5
*/
const STRINGS = 10e4;
const CYCLES = 10e5;
static $strings = array();
static $result = array();
$timer = time();
for ($i = 0; $i <= STRINGS; $i++) {
echo "\r", $i, '/', STRINGS;
if ($i % 20 === 0) {
$time = (time() - $timer) / ($i + 1);
$left = STRINGS - $i;
$total_seconds = (int) ($time * $left);
$hours = (int) ($total_seconds / 3600);
$minutes = (int) ($total_seconds % 3600 / 60);
$seconds = $total_seconds - $hours * 3600 - $minutes * 60;
echo "\t Time left: ";
if ($hours > 0) {
echo "$hours hours, ";
}
if ($minutes > 0) {
echo "$minutes minutes, ";
}
echo "$seconds seconds";
}
$result[$i] = $strings[$i] = generateString();
for ($k = 0; $k <= CYCLES; $k++) {
$result[$i] = md5($result[$i]);
}
}
for ($i = 0; $i < count($result); $i++) {
for ($k = $i; $k < count($result); $k++) {
if ($result[$i] === $result[$k]
&& $strings[$i] !== $strings[$k]) {
echo "\033[1;37;42m";
echo 'Hash cycling exists! ';
echo "\033[0m";
echo "\n Strings:\n\t", $strings[$i], "\n\t", $strings[$k];
echo "\n Hashes:\n\t", $result[$i], "\n\t", $result[$k], "\n";
}
}
}
function generateString($length = 40) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$count = strlen($characters);
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters{mt_rand(0, $count)};
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment