]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - passencrypt.php
Add public/private
[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 function rand_ascii($length = 1)
30 {
31     $s = "";
32     for ($i = 1; $i <= $length; $i++) {
33         // return only typeable 7 bit ascii, avoid quotes
34         $s .= chr(mt_rand(40, 126));
35     }
36     return $s;
37 }
38
39 ////
40 // Function to create better user passwords (much larger keyspace),
41 // suitable for user passwords.
42 // Sequence of random ASCII numbers, letters and some special chars.
43 // Note: There exist other algorithms for easy-to-remember passwords.
44 function random_good_password($minlength = 5, $maxlength = 8)
45 {
46     $newpass = '';
47     // assume ASCII ordering (not valid on EBCDIC systems!)
48     $valid_chars = "!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
49     $start = ord($valid_chars);
50     $end = ord(substr($valid_chars, -1));
51     $length = mt_rand($minlength, $maxlength);
52     while ($length > 0) {
53         $newchar = mt_rand($start, $end);
54         if (!strrpos($valid_chars, $newchar))
55             continue; // skip holes
56         $newpass .= sprintf("%c", $newchar);
57         $length--;
58     }
59     return $newpass;
60 }
61
62 /** PHP5 deprecated old-style globals if !(bool)ini_get('register_long_arrays').
63  *  See Bug #1180115
64  * We want to work with those old ones instead of the new superglobals,
65  * for easier coding.
66  */
67 foreach (array('SERVER', 'GET', 'POST', 'ENV') as $k) {
68     if (!isset($GLOBALS['HTTP_' . $k . '_VARS']) and isset($GLOBALS['_' . $k]))
69         $GLOBALS['HTTP_' . $k . '_VARS'] =& $GLOBALS['_' . $k];
70 }
71 unset($k);
72
73 $posted = $GLOBALS['HTTP_POST_VARS'];
74 if (!empty($posted['create'])) {
75     $new_password = random_good_password();
76     echo "<p>The newly created random password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<samp><strong>",
77     htmlentities($new_password), "</strong></samp></p>\n";
78     $posted['password'] = $new_password;
79     $posted['password2'] = $new_password;
80 }
81
82 if (($posted['password'] != "")
83     && ($posted['password'] == $posted['password2'])
84 ) {
85     $password = $posted['password'];
86     /**
87      * http://www.php.net/manual/en/function.crypt.php
88      */
89     // Use the maximum salt length the system can handle.
90     $salt_length = max(CRYPT_SALT_LENGTH,
91         2 * CRYPT_STD_DES,
92         9 * CRYPT_EXT_DES,
93         12 * CRYPT_MD5,
94         16 * CRYPT_BLOWFISH);
95     // Generate the encrypted password.
96     $encrypted_password = crypt($password, rand_ascii($salt_length));
97     $debug = $HTTP_GET_VARS['debug'];
98     if ($debug)
99         echo "The password was encrypted using a salt length of: $salt_length<br />\n";
100     echo "<p>The encrypted password is:<br />\n<br />&nbsp;&nbsp;&nbsp;\n<samp><strong>",
101     htmlentities($encrypted_password), "</strong></samp></p>\n";
102     echo "<hr />\n";
103 } elseif ($posted['password'] != "") {
104     echo "The passwords did not match. Please try again.<br />\n";
105 }
106 if (empty($REQUEST_URI))
107     $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
108 if (empty($REQUEST_URI))
109     $REQUEST_URI = $_SERVER['REQUEST_URI'];
110 ?>
111
112 <form action="<?php echo $REQUEST_URI ?>" method="post">
113     <fieldset>
114         <legend>Encrypt</legend>
115         Enter a password twice to encrypt it:<br/>
116         <input type="password" name="password" value=""/><br/>
117         <input type="password" name="password2" value=""/> <input type="submit" value="Encrypt"/>
118     </fieldset>
119     <br/>
120     or:<br/>
121     <br/>
122     <fieldset>
123         <legend>Generate</legend>
124         Create a new random password: <input type="submit" name="create" value="Create"/>
125     </fieldset>
126 </form>
127 </body>
128 </html>