4 <meta charset="UTF-8" />
5 <title>Password Encryption Tool</title>
7 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
9 This file is part of PhpWiki.
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.
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.
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.
27 <h1>Password Encryption Tool</h1>
29 function rand_ascii($length = 1)
32 for ($i = 1; $i <= $length; $i++) {
33 // return only typeable 7 bit ascii, avoid quotes
34 $s .= chr(mt_rand(40, 126));
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)
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);
53 $newchar = mt_rand($start, $end);
54 if (!strrpos($valid_chars, $newchar))
55 continue; // skip holes
56 $newpass .= sprintf("%c", $newchar);
62 /** PHP5 deprecated old-style globals if !(bool)ini_get('register_long_arrays').
64 * We want to work with those old ones instead of the new superglobals,
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];
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 /> \n<samp><strong>",
77 htmlentities($new_password), "</strong></samp></p>\n";
78 $posted['password'] = $new_password;
79 $posted['password2'] = $new_password;
82 if (($posted['password'] != "")
83 && ($posted['password'] == $posted['password2'])
85 $password = $posted['password'];
87 * http://www.php.net/manual/en/function.crypt.php
89 // Use the maximum salt length the system can handle.
90 $salt_length = max(CRYPT_SALT_LENGTH,
95 // Generate the encrypted password.
96 $encrypted_password = crypt($password, rand_ascii($salt_length));
97 $debug = $HTTP_GET_VARS['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 /> \n<samp><strong>",
101 htmlentities($encrypted_password), "</strong></samp></p>\n";
103 } elseif ($posted['password'] != "") {
104 echo "The passwords did not match. Please try again.<br />\n";
106 if (empty($REQUEST_URI))
107 $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
108 if (empty($REQUEST_URI))
109 $REQUEST_URI = $_SERVER['REQUEST_URI'];
112 <form action="<?php echo $REQUEST_URI ?>" method="post">
114 <legend accesskey="P">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"/>
123 <legend accesskey="C">Generate</legend>
124 Create a new random password: <input type="submit" name="create" value="Create"/>