]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - passencrypt.php
fix passencrypt
[SourceForge/phpwiki.git] / passencrypt.php
1 <?php printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", 'iso-8859-1'); ?>
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 <!-- $Id: passencrypt.php,v 1.5 2004-04-21 04:45:35 rurban Exp $ -->
8 <title>Password Encryption Tool</title>
9 <!--
10 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
11
12 This file is part of PhpWiki.
13
14 PhpWiki is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 PhpWiki is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with PhpWiki; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 -->
28 </head>
29 <body>
30 <h1>Password Encryption Tool</h1>
31 <?php
32 /**
33  * Seed the random number generator.
34  *
35  * better_srand() ensures the randomizer is seeded only once.
36  * 
37  * How random do you want it? See:
38  * http://www.php.net/manual/en/function.srand.php
39  * http://www.php.net/manual/en/function.mt-srand.php
40  */
41 function better_srand($seed = '') {
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     better_srand();
62     $s = "";
63     for ($i = 1; $i <= $length; $i++) {
64         // return only typeable 7 bit ascii, avoid quotes
65         if (function_exists('mt_rand'))
66             // the usually bad glibc srand()
67             $s .= chr(mt_rand(40, 126)); 
68         else
69             $s .= chr(rand(40, 126));
70     }
71     return $s;
72 }
73
74 ////
75 // Function to create better user passwords (much larger keyspace),
76 // suitable for user passwords.
77 // Sequence of random ASCII numbers, letters and some special chars.
78 // Note: There exist other algorithms for easy-to-remember passwords.
79 function random_good_password ($minlength = 5, $maxlength = 8) {
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 $posted = $GLOBALS['HTTP_POST_VARS'];
104 if (!empty($posted['create'])) {
105     $new_password = random_good_password();
106     echo "<p>The newly created random password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<strong>",
107          htmlentities($new_password),"</strong></p>\n";
108     $posted['password'] = $new_password;
109     $posted['password2'] = $new_password;
110 }
111
112 if (($posted['password'] != "")
113     && ($posted['password'] == $posted['password2'])) {
114     $password = $posted['password'];
115     /**
116      * http://www.php.net/manual/en/function.crypt.php
117      */
118     // Use the maximum salt length the system can handle.
119     $salt_length = max(CRYPT_SALT_LENGTH,
120                         2 * CRYPT_STD_DES,
121                         9 * CRYPT_EXT_DES,
122                        12 * CRYPT_MD5,
123                        16 * CRYPT_BLOWFISH);
124     // Generate the encrypted password.
125     $encrypted_password = crypt($password, rand_ascii($salt_length));
126     $debug = $HTTP_GET_VARS['debug'];
127     if ($debug)
128         echo "The password was encrypted using a salt length of: $salt_length<br />\n";
129     echo "<p>The encrypted password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<strong>",
130          htmlentities($encrypted_password),"</strong></p>\n";
131     echo "<hr />\n";
132 }
133 else if ($posted['password'] != "") {
134     echo "The passwords did not match. Please try again.<br />\n";
135 }
136 if (empty($REQUEST_URI))
137     $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
138 if (empty($REQUEST_URI))
139     $REQUEST_URI = $HTTP_SERVER_VARS['REQUEST_URI'];
140 ?>
141
142 <form action="<?php echo $REQUEST_URI ?>" method="post">
143 <fieldset><legend accesskey="P">Encrypt</legend>
144 Enter a password twice to encrypt it:<br />
145 <input type="password" name="password" value="" /><br />
146 <input type="password" name="password2" value="" /> <input type="submit" value="Encrypt" />
147 </fieldset>
148 <br />
149 or:<br />
150 <br />
151 <fieldset><legend accesskey="C">Generate </legend>
152 Create a new random password: <input type="submit" name="create" value="Create" />
153 </fieldset>
154 </form>
155 </body>
156 </html>