-
password generator code
1) CODE TITLE: pissedandhappy password gen
2) AUTHOR NAME/NOTES: Peter
3) DESCRIPTION: this code complicated java code produces a number of random passwords, i currently can not find a good use for it but i think that it will be good for some sort of user database. Please tell me if you do use this code by emailing me at micklep@hotmail.com
4) URL TO CODE: http://www.stuff.pissedandhappy.co.uk/password-gen.txt
-
It's javascript, so implementation in a database would be impossible, without recoding to php, asp, etc.
I'm confused by the various scripts... why is it setup in so many seperate <script></script> elements? Seems like needless extra stuff to me.
Really, you've just kinda randomly assigned things, but you could create a real random script by using the ordinal values for characters (0-9,a-z,A-Z, and symbols, etc. if you wanted) and choosing one at random.
Here's the PHP--
PHP Code:
<?php
$len = 8; //set pass length //in this case, 8
for ($n=0;$n<$len;$n++) { //loop starting at 0, while less than len value
$ord = rand(1,62); //10num+26LET+26let //add more if you want
if ($ord <= 10) {$ord += 47;} //numbers
else if ($ord <= 36) {$ord += 54;} //LETTERS
else { $ord += 60; } //letters
//include other sets of chars if you want
$pass .= chr($ord); //add char to existing password
}
echo $pass; //output password
?>
Untested, but should work just fine.
If we were to compare the outputs, they would differ in complexity and randomness. Since you are using a much more limited set of characters, the inclusive code above would generate many more possible outcomes. Yours can work too, but this just has more possibilities.
I don't do JS, but I'm sure there's a way to do this as well.
You could just output as html, and use &#nn; for example.
EDIT: Consider this a submission if it seems worthy.
EDIT AGAIN: Ok, code fixed above...
-
http://ci-pro.com/misc/phptest/passgen.php
That's a test page.
I had an error in the script earlier, but it's fixed now.
I was adding the full value for each set, like 64 for capital letters, but then realized that to be wrong since that set of random values starts at 10, so needed to subtract that first. For the next, subtract 10+26, so not 96, but 60.
-
-
Hmm.... here's a more logical way to look at the code:
PHP Code:
<?php
$len = 8; //set pass length //in this case, 8
for ($n=0;$n<$len;$n++) { //loop starting at 0, while less than len value
$ord = rand(1,62); //10num+26LET+26let //add more if you want
if ($ord <= 10) {
$ord += 47; //numbers
}
else {
$ord -= 10;
if ($ord <= 26) {
$ord += 64; //LETTERS
}
else {
$ord -= 26;
$ord += 96; //letters
}
}
//include other sets of chars if you want
$pass .= chr($ord); //add char to existing password
}
echo $pass; //output password
?>
The math was weird, and it confused me, so it's bound to confuse others, considering I wrote the script in the first place.
Hmm.... I might write that into an array/foreach function so you an specify character sets within the array (which would, interestingly enough, be a 3D array ;))
-
in answer to your second post, the reason i did it in so many seperate script is because i was brand new to javascript.
Thanks to you i now know that i can put them all together in 1 set of code. I have produced another script that i think is rather fun:
DONT LOOK IF EASILY OFFENDED:
http://www.stuff.pissedandhappy.co.uk/insult.html
Tell me what you think, i added some html to spruce it up a bit but it seems to really affect loading time :(
Cheers
-
-
-
my variant is
PHP Code:
<?
function exPassGen( $passlen = 8 )
{
$lets = array(
0 => range('a', 'z'),
1 => range('A', 'Z'),
2 => range('0', '9'),
3 => array('~', '!', '#', '@', '$', '^', ':', '(', ')', '-', '_', '+', '='),
);
$ret = '';
for( $i = 0; $i < $passlen; $i++ )
{
$lrnd = rand(0, count($lets)-1 );
$ret .= $lets[$lrnd][ rand(0,count($lets[$lrnd])-1) ];
}
return $ret;
}
echo exPassGen( 10 )."<br/>";
echo exPassGen( 5 );
?>
-
Hmm.... perhaps. But most of those symbols aren't valid as passwords, if you actually want to use that. You could use up to 255 characters, but many wouldn't work. Depends on the exact system, though.
Also, using the ascii codes really makes sense in this case, since it can be changed fairly easily. But doing it like that could work as well.
Do you have a test page?