...

View Full Version : Simple password generator



p4plus2
03-03-2008, 11:38 PM
Demo: http://p4plus2.co.cc/pass/password.php

source:


<html>
<head>
<title></title>
</head>
<body>
<?php
$passwordgenerate = array(
1 => "`",
2 => "~",
3 => "!",
4 => "@",
5 => "#",
6 => "$",
7 => "&#37;",
8 => "^",
9 => "&",
10 => "*",
11 => "(",
12 => ")",
13 => "_",
14 => "-",
15 => "=",
16 => "+",
17 => "{",
18 => "}",
19 => "[",
20 => "]",
21 => "|",
22 => "\\",
23 => ":",
24 => ";",
25 => "\"",
26 => "'",
27 => "/",
28 => "?",
29 => ".",
30 => ">",
31 => "<",
32 => ",",
33 => "0",
34 => "1",
35 => "2",
36 => "3",
37 => "4",
38 => "5",
39 => "6",
40 => "7",
41 => "8",
42 => "9",
43 => "q",
44 => "w",
45 => "e",
46 => "r",
47 => "t",
48 => "y",
49 => "u",
50 => "i",
51 => "o",
52 => "p",
53 => "a",
54 => "s",
55 => "d",
56 => "f",
57 => "g",
58 => "h",
59 => "j",
60 => "k",
61 => "l",
62 => "z",
63 => "x",
64 => "c",
65 => "v",
66 => "b",
67 => "n",
68 => "m",
69 => "Q",
70 => "W",
71 => "E",
72 => "R",
73 => "T",
74 => "Y",
75 => "U",
76 => "I",
77 => "O",
78 => "P",
79 => "A",
80 => "S",
81 => "D",
82 => "F",
83 => "G",
84 => "H",
85 => "J",
86 => "K",
87 => "L",
88 => "Z",
89 => "X",
90 => "C",
91 => "V",
92 => "B",
93 => "N",
94 => "M"
);
$passwordlength = $_POST['length'];
$type = $_POST['type'];
function type(){
global $type, $passwordgenerate, $password, $passwordlength;
$i = 0;
if($type == all){
while($i < $passwordlength){
$number = rand(1,94);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == letters){
while($i < $passwordlength){
$number = rand(43,94);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == caps){
while($i < $passwordlength){
$number = rand(69,94);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == low){
while($i < $passwordlength){
$number = rand(43,68);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == numbers){
while($i < $passwordlength){
$number = rand(33,42);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == symbols){
while($i < $passwordlength){
$number = rand(1,32);
$password .= $passwordgenerate["$number"];
$i++;
}
return $password;
}
elseif($type == extreme){
while($i < $passwordlength){
$number = rand(1,94);
$password1 = md5(str_rot13(md5(sha1($passwordgenerate["$number"]))));
$password .= $password1;
$i++;
}
return $password;
}
else{
echo "hacking attempt *or a internal error in which case refresh the page*";
die;

}
}
if($passwordlength != ''){
type();
}
echo("<form action=\"password.php\" method=\"post\">pass length: <input name=\"length\" maxlength=\"4\" value=\""."$passwordlength"."\" /><br />pass type: <select name=\"type\"><option value=\"all\">all</option><option value=\"letters\">a-z(caps and lower)</option><option value=\"caps\">Letters a-z (caps)</option><option value=\"low\">letters a-z(lower)</option><option value=\"numbers\">numbers 0-9</option><option value=\"symbols\">All symbols on keyboard</option><option value=\"extreme\">extreme*</option></select><br /><input type=\"submit\" /></form>");
echo("$password");
?>
<p>
<br /><br />
* extreme is longer in length than what you input and is heavily encrypted letters and numbers.!
</p>
</body>
</html>



I know I could have used a for each statement and all those goodies but I did it the long way since it is easier to update in the future. post feedback!

Thanks,
~p4plus2~

Inigoesdr
03-04-2008, 01:09 AM
I know I could have used a for each statement and all those goodies but I did it the long way since it is easier to update in the future. post feedback!
You could have used a for() loop for the password generation instead of a while(), too. What is going to be easier to update later using this method? Also the global keyword isn't needed since you can pass the values as arguments to the function, or pass them by reference if you intend to modify them.

p4plus2
03-04-2008, 01:44 AM
I use global since when I deal with phpbb(2.0.*) a lot. we are supposed to use global in our functions for variables. old habits. for and while loops do just about the same thing only real difference is syntax I didn't see any real benefit for a for statement here.

For updating I plan on adding special characters to the array and if I used for each it just becomes a mess since I'll need to redo my loop. Call me crazy as well but I tend to like doing things the hard way gives more satisfaction than a short cut.

Inigoesdr
03-04-2008, 03:51 AM
for and while loops do just about the same thing only real difference is syntax I didn't see any real benefit for a for statement here.
Using a while() loop like you do here is similar to using a for() loop, however, you can do the same thing this code does:

$i = 0;
while($i < $passwordlength)
{
$number = rand(1,94);
$password .= $passwordgenerate["$number"];
$i++;
}
Like this:

for($i = 0; $i < $passwordlength; $i++)
$password .= $passwordgenerate[rand(1, 94)];
This situation is exactly what a for() loop is for.

p4plus2
03-04-2008, 05:18 AM
Back to my point though what are the real benefits thats what I questioned since they still do the same thing. yah they are small, but do they effect anything, since I never noticed a diff when I used em....and shouldn't the code be:



for($i = 0; $i < $passwordlength; $i++){
$password .= $passwordgenerate[rand(1, 94)];
}

_Aerospace_Eng_
03-04-2008, 05:36 AM
No need for the brackets if the rest of the loop is just one line. The loop will read only the first line if no brackets are in place.

Inigoesdr
03-04-2008, 06:00 AM
Back to my point though what are the real benefits thats what I questioned since they still do the same thing. yah they are small, but do they effect anything, since I never noticed a diff when I used em
Well, the for() loop is slightly faster(~0.006 secs), and the syntax is better suited for this type of loop, but other than that they're functionally equivalent in this case.

p4plus2
03-04-2008, 03:13 PM
ah....well I may convert it later when I'm not so busy then :) but it is lower on my to-do list since it is already working. I have a lot of things I am working on atm so I'd like to get em working first.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum