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