]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - passencrypt.php
php_closing_tag [PSR-2] The closing ?> tag MUST be omitted from files containing...
[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     static $wascalled = FALSE;
42     if (!$wascalled) {
43         if ($seed === '') {
44             list($usec, $sec) = explode(" ", microtime());
45             if ($usec > 0.1)
46                 $seed = (double) $usec * $sec;
47             else // once in a while use the combined LCG entropy
48                 $seed = (double) 1000000 * substr(uniqid("", true), 13);
49         }
50         if (function_exists('mt_srand')) {
51             mt_srand($seed); // mersenne twister
52         } else {
53             srand($seed);
54         }
55         $wascalled = TRUE;
56     }
57 }
58
59 function rand_ascii($length = 1) {
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     $newpass = '';
80     // assume ASCII ordering (not valid on EBCDIC systems!)
81     $valid_chars = "!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
82     $start = ord($valid_chars);
83     $end   = ord(substr($valid_chars, -1));
84     better_srand();
85     if (function_exists('mt_rand')) // mersenne twister
86         $length = mt_rand($minlength, $maxlength);
87     else        // the usually bad glibc rand()
88         $length = rand($minlength, $maxlength);
89     while ($length > 0) {
90         if (function_exists('mt_rand'))
91             $newchar = mt_rand($start, $end);
92         else
93             $newchar = rand($start, $end);
94         if (! strrpos($valid_chars, $newchar) )
95             continue; // skip holes
96         $newpass .= sprintf("%c", $newchar);
97         $length--;
98     }
99     return $newpass;
100 }
101
102 /** PHP5 deprecated old-style globals if !(bool)ini_get('register_long_arrays').
103   *  See Bug #1180115
104   * We want to work with those old ones instead of the new superglobals,
105   * for easier coding.
106   */
107 foreach (array('SERVER','GET','POST','ENV') as $k) {
108     if (!isset($GLOBALS['HTTP_'.$k.'_VARS']) and isset($GLOBALS['_'.$k]))
109         $GLOBALS['HTTP_'.$k.'_VARS'] =& $GLOBALS['_'.$k];
110 }
111 unset($k);
112
113 $posted = $GLOBALS['HTTP_POST_VARS'];
114 if (!empty($posted['create'])) {
115     $new_password = random_good_password();
116     echo "<p>The newly created random password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<tt><strong>",
117          htmlentities($new_password),"</strong></tt></p>\n";
118     $posted['password'] = $new_password;
119     $posted['password2'] = $new_password;
120 }
121
122 if (($posted['password'] != "")
123     && ($posted['password'] == $posted['password2'])) {
124     $password = $posted['password'];
125     /**
126      * http://www.php.net/manual/en/function.crypt.php
127      */
128     // Use the maximum salt length the system can handle.
129     $salt_length = max(CRYPT_SALT_LENGTH,
130                         2 * CRYPT_STD_DES,
131                         9 * CRYPT_EXT_DES,
132                        12 * CRYPT_MD5,
133                        16 * CRYPT_BLOWFISH);
134     // Generate the encrypted password.
135     $encrypted_password = crypt($password, rand_ascii($salt_length));
136     $debug = $HTTP_GET_VARS['debug'];
137     if ($debug)
138         echo "The password was encrypted using a salt length of: $salt_length<br />\n";
139     echo "<p>The encrypted password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<tt><strong>",
140          htmlentities($encrypted_password),"</strong></tt></p>\n";
141     echo "<hr />\n";
142 }
143 else if ($posted['password'] != "") {
144     echo "The passwords did not match. Please try again.<br />\n";
145 }
146 if (empty($REQUEST_URI))
147     $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
148 if (empty($REQUEST_URI))
149     $REQUEST_URI = $HTTP_SERVER_VARS['REQUEST_URI'];
150 ?>
151
152 <form action="<?php echo $REQUEST_URI ?>" method="post">
153 <fieldset><legend accesskey="P">Encrypt</legend>
154 Enter a password twice to encrypt it:<br />
155 <input type="password" name="password" value="" /><br />
156 <input type="password" name="password2" value="" /> <input type="submit" value="Encrypt" />
157 </fieldset>
158 <br />
159 or:<br />
160 <br />
161 <fieldset><legend accesskey="C">Generate </legend>
162 Create a new random password: <input type="submit" name="create" value="Create" />
163 </fieldset>
164 </form>
165 </body>
166 </html>