]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - passencrypt.php
Reformat code
[SourceForge/phpwiki.git] / passencrypt.php
1 <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"; ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
7     <title>Password Encryption Tool</title>
8     <!--
9     Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
10
11     This file is part of PhpWiki.
12
13     PhpWiki is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     PhpWiki is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License along
24     with PhpWiki; if not, write to the Free Software Foundation, Inc.,
25     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26     -->
27 </head>
28 <body>
29 <h1>Password Encryption Tool</h1>
30 <?php
31 /**
32  * Seed the random number generator.
33  *
34  * better_srand() ensures the randomizer is seeded only once.
35  *
36  * How random do you want it? See:
37  * http://www.php.net/manual/en/function.srand.php
38  * http://www.php.net/manual/en/function.mt-srand.php
39  */
40 function better_srand($seed = '')
41 {
42     static $wascalled = FALSE;
43     if (!$wascalled) {
44         if ($seed === '') {
45             list($usec, $sec) = explode(" ", microtime());
46             if ($usec > 0.1)
47                 $seed = (double)$usec * $sec;
48             else // once in a while use the combined LCG entropy
49                 $seed = (double)1000000 * substr(uniqid("", true), 13);
50         }
51         if (function_exists('mt_srand')) {
52             mt_srand($seed); // mersenne twister
53         } else {
54             srand($seed);
55         }
56         $wascalled = TRUE;
57     }
58 }
59
60 function rand_ascii($length = 1)
61 {
62     better_srand();
63     $s = "";
64     for ($i = 1; $i <= $length; $i++) {
65         // return only typeable 7 bit ascii, avoid quotes
66         if (function_exists('mt_rand'))
67             // the usually bad glibc srand()
68             $s .= chr(mt_rand(40, 126));
69         else
70             $s .= chr(rand(40, 126));
71     }
72     return $s;
73 }
74
75 ////
76 // Function to create better user passwords (much larger keyspace),
77 // suitable for user passwords.
78 // Sequence of random ASCII numbers, letters and some special chars.
79 // Note: There exist other algorithms for easy-to-remember passwords.
80 function random_good_password($minlength = 5, $maxlength = 8)
81 {
82     $newpass = '';
83     // assume ASCII ordering (not valid on EBCDIC systems!)
84     $valid_chars = "!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
85     $start = ord($valid_chars);
86     $end = ord(substr($valid_chars, -1));
87     better_srand();
88     if (function_exists('mt_rand')) // mersenne twister
89         $length = mt_rand($minlength, $maxlength);
90     else // the usually bad glibc rand()
91         $length = rand($minlength, $maxlength);
92     while ($length > 0) {
93         if (function_exists('mt_rand'))
94             $newchar = mt_rand($start, $end);
95         else
96             $newchar = rand($start, $end);
97         if (!strrpos($valid_chars, $newchar))
98             continue; // skip holes
99         $newpass .= sprintf("%c", $newchar);
100         $length--;
101     }
102     return $newpass;
103 }
104
105 /** PHP5 deprecated old-style globals if !(bool)ini_get('register_long_arrays').
106  *  See Bug #1180115
107  * We want to work with those old ones instead of the new superglobals,
108  * for easier coding.
109  */
110 foreach (array('SERVER', 'GET', 'POST', 'ENV') as $k) {
111     if (!isset($GLOBALS['HTTP_' . $k . '_VARS']) and isset($GLOBALS['_' . $k]))
112         $GLOBALS['HTTP_' . $k . '_VARS'] =& $GLOBALS['_' . $k];
113 }
114 unset($k);
115
116 $posted = $GLOBALS['HTTP_POST_VARS'];
117 if (!empty($posted['create'])) {
118     $new_password = random_good_password();
119     echo "<p>The newly created random password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<tt><strong>",
120     htmlentities($new_password), "</strong></tt></p>\n";
121     $posted['password'] = $new_password;
122     $posted['password2'] = $new_password;
123 }
124
125 if (($posted['password'] != "")
126     && ($posted['password'] == $posted['password2'])
127 ) {
128     $password = $posted['password'];
129     /**
130      * http://www.php.net/manual/en/function.crypt.php
131      */
132     // Use the maximum salt length the system can handle.
133     $salt_length = max(CRYPT_SALT_LENGTH,
134         2 * CRYPT_STD_DES,
135         9 * CRYPT_EXT_DES,
136         12 * CRYPT_MD5,
137         16 * CRYPT_BLOWFISH);
138     // Generate the encrypted password.
139     $encrypted_password = crypt($password, rand_ascii($salt_length));
140     $debug = $HTTP_GET_VARS['debug'];
141     if ($debug)
142         echo "The password was encrypted using a salt length of: $salt_length<br />\n";
143     echo "<p>The encrypted password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<tt><strong>",
144     htmlentities($encrypted_password), "</strong></tt></p>\n";
145     echo "<hr />\n";
146 } else if ($posted['password'] != "") {
147     echo "The passwords did not match. Please try again.<br />\n";
148 }
149 if (empty($REQUEST_URI))
150     $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
151 if (empty($REQUEST_URI))
152     $REQUEST_URI = $HTTP_SERVER_VARS['REQUEST_URI'];
153 ?>
154
155 <form action="<?php echo $REQUEST_URI ?>" method="post">
156     <fieldset>
157         <legend accesskey="P">Encrypt</legend>
158         Enter a password twice to encrypt it:<br/>
159         <input type="password" name="password" value=""/><br/>
160         <input type="password" name="password2" value=""/> <input type="submit" value="Encrypt"/>
161     </fieldset>
162     <br/>
163     or:<br/>
164     <br/>
165     <fieldset>
166         <legend accesskey="C">Generate</legend>
167         Create a new random password: <input type="submit" name="create" value="Create"/>
168     </fieldset>
169 </form>
170 </body>
171 </html>