PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/gforge/plugins/wiki/www/passencrypt.php

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