Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created June 15, 2016 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloberg/9e03da8ed37feebfb176c1c0eed8d286 to your computer and use it in GitHub Desktop.
Save mloberg/9e03da8ed37feebfb176c1c0eed8d286 to your computer and use it in GitHub Desktop.
<?php
function compareHashes($hashOne, $hashTwo)
{
$result = 0;
for ($i = 0; $i < strlen($hashOne); $i++) {
$charOne = substr($hashOne, $i, 1);
$charTwo = substr($hashTwo, $i, 1);
$intOne = base_convert($charOne, 16, 10);
$intTwo = base_convert($charTwo, 16, 10);
$diff = abs($intOne - $intTwo);
$result = $result + $diff;
}
return $result;
}
function controller($timeInMs, $seed)
{
$startTime = microtime(true);
$seedHash = md5($seed);
$resultString = null;
$resultDiff = null;
$iterations = 0;
while ((microtime(true) - $startTime) < ($timeInMs / 1000)) {
$iterations++;
$randomString = bin2hex(random_bytes(4));
$randomHashCompare = compareHashes($seedHash, md5($randomString));
if (is_null($resultDiff) || $randomHashCompare < $resultDiff) {
$resultDiff = $randomHashCompare;
$resultString = $randomString;
}
}
return [
'string' => $resultString,
'diff' => $resultDiff,
'iterations' => $iterations,
];
}
function generateCsv(array $comparisons)
{
echo implode(',', ['Time', 'Seed', 'Comparisons', 'Diff', 'Nonce']) . PHP_EOL;
foreach ($comparisons as $word => $time) {
$comparison = controller($time, $word);
echo implode(',', [$time, $word, $comparison['iterations'], $comparison['diff'], $comparison['string']]) . PHP_EOL;
}
// $csv = '';
//
// foreach ($results as $result) {
// $csv .= implode(',', $result) . "\n";
// }
//
// return $csv;
}
//var_dump(compareHashes('3b2f', '78a2'));
//var_dump(compareHashes('173d8235', 'ac83ff63'));
//Time,Seed,Comparisons,Diff,Nonce
//25,"madwire",?,?,?
//50,"chello",?,?,?
//100,"donkey",?,?,?
//200,"biscuits",?,?,?
//400,"gerkins",?,?,?
//800,"trusty",?,?,?
//1600,"sanchez",?,?,?
//3200,"wigwam",?,?,?
//6400,"boinker",?,?,?
//12800,"vader",?,?,?
//25600,"blumpkin",?,?,?
//51200,"kitten",?,?,?
//102400,"gymshorts",?,?,?
//204800,"shiftybuckets",?,?,?
//409600,"stompydumper",?,?,?
//819200,"krunkbiscuits",?,?,?
$comparisons = [
'madwire' => 25,
'chello' => 50,
'donkey' => 100,
'biscuits' => 200,
'gerkins' => 400,
'trusty' => 800,
'sanchez' => 1600,
'wigwam' => 3200,
'boinker' => 6400,
'vader' => 12800,
'blumpkin' => 25600,
'kitten' => 51200,
'gymshorts' => 102400,
'shiftybuckets' => 204800,
'stompydumper' => 409600,
'krunkbiscuits' => 819200,
];
generateCsv($comparisons);
//var_dump(controller(100, 'madwire'));
//var_dump(controller(1000, 'madwire'));
//var_dump(controller(10000, 'madwire'));
//echo md5('madwire').PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment