note 76613 deleted from function.md5 by danbrown
| From: | danbrown@php.net | Date: | Mon, 20 Jun 2011 15:33:00 +0000 |
| Subject: | note 76613 deleted from function.md5 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180781@lists.php.net to get a copy of this message | ||
Note Submitter: the-master at supanet dot com
----
@ me at dontemailme dot com
Your code takes ages to run mainly because it uses totally random characters, which in some cases
don't exist because you have used chr() and rand() which can output very large numbers.
I think most people only use alphanumeric passwords so here is a code which is far quicker. Where
yours took 16 seconds mine took 2. It still takes ages for large passwords though which is
reassuring! Also a hacker shouldn't know the password length meaning that the passwords are
very secure with hashing.
<?php
set_time_limit(0);
function randomkeys($length)
{
$pattern = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$key = $pattern{rand(0,61)};
for($i=1;$i<$length;$i++)
{
$key .= $pattern{rand(0,61)};
}
return $key;
}
if (isset($_GET['password'])){
$password = $_GET['password'];
$password_length = strlen($password);
}
else
{
$password_length = 3;
if (isset($_GET['length'])){
$password_length = $_GET['length'];}
$password = randomkeys($password_length);
}
echo "Password is: $password <br />";
$password = md5($password);
$attempts = 0;
$start = microtime(true);
while ($password != $guess){
$guess = md5(randomkeys($password_length));
$attempts++;
}
$end = microtime(true);
$time = $end-$start;
echo "Password guessed correctly after $attempts attempts and $time seconds";
?>
I think this code is correct but hopefully someone will tell me if it isn't or change it.