|
From: <car...@us...> - 2025-02-13 23:58:13
|
Revision: 11091
http://sourceforge.net/p/phpwiki/code/11091
Author: carstenklapp
Date: 2025-02-13 23:58:12 +0000 (Thu, 13 Feb 2025)
Log Message:
-----------
Improve password encryption and random pw generation, added copy to clipboard buttons
Modified Paths:
--------------
trunk/passencrypt.php
Modified: trunk/passencrypt.php
===================================================================
--- trunk/passencrypt.php 2025-02-13 23:24:53 UTC (rev 11090)
+++ trunk/passencrypt.php 2025-02-13 23:58:12 UTC (rev 11091)
@@ -2,9 +2,9 @@
<html xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
- <title>Password Encryption Tool</title>
+ <title>Password Encryption Tool 2</title>
<!--
- Copyright © 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
+ Copyright © 1999, 2000, 2001, 2002-2025 $ThePhpWikiProgrammingTeam
This file is part of PhpWiki.
@@ -25,9 +25,53 @@
SPDX-License-Identifier: GPL-2.0-or-later
-->
+<style type="text/css">
+<!--
+body {
+ color: black;
+ background: white;
+ border-top: 1px solid #7c7c7c;
+ border-left: 1px solid #c3c3c3;
+ border-bottom: 1px solid #dddddd;
+ border-right: 1px solid #c3c3c3;
+ padding-left: 0.8em;
+ padding-right: 0.8em;
+ padding-top: 0.5em;
+ padding-bottom: 0.5em;
+
+ margin: 2ex;
+}
+p {
+ line-height: 4ex;
+}
+samp,
+.filename {
+ font-family: Monaco, monospace;
+ font-weight: bold;
+}
+samp {
+ background: white;
+ border-top: 1px solid #7c7c7c;
+ border-left: 1px solid #c3c3c3;
+ border-bottom: 1px solid #dddddd;
+ border-right: 1px solid #c3c3c3;
+ padding-left: 0.8em;
+ padding-right: 0.8em;
+ padding-top: 0.5em;
+ padding-bottom: 0.5em;
+ margin: 0.5ex 0;
+ margin-left: 1ex;
+ clear: both;
+ display: table;
+}
+fieldset {
+ display: inline;
+}
+-->
+</style>
</head>
<body>
-<h1>Password Encryption Tool</h1>
+<h1>Password Encryption Tool 2</h1>
<?php
function rand_ascii($length = 1)
{
@@ -44,11 +88,13 @@
// suitable for user passwords.
// Sequence of random ASCII numbers, letters and some special chars.
// Note: There exist other algorithms for easy-to-remember passwords.
-function random_good_password($minlength = 5, $maxlength = 8)
+function random_good_password($minlength = 6, $maxlength = 64)
{
- $newpass = '';
+ $generated_pw = '';
// assume ASCII ordering (not valid on EBCDIC systems!)
- $valid_chars = "!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
+ $valid_chars = "!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; //includees some special chars
+ //$valid_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";//keep it simple
+ //return substr(str_shuffle($valid_chars), 0, $maxlength);//basic method
$start = ord($valid_chars);
$end = ord(substr($valid_chars, -1));
$length = mt_rand($minlength, $maxlength);
@@ -57,12 +103,40 @@
if (!strrpos($valid_chars, $newchar)) {
continue;
} // skip holes
- $newpass .= sprintf("%c", $newchar);
+ $generated_pw .= sprintf("%c", $newchar);
$length--;
}
- return $newpass;
+ return $generated_pw;
}
+/**
+ * Generate a random string, using a cryptographically secure
+ * pseudorandom number generator (random_int)
+ *
+ * For PHP 7, random_int is a PHP core function
+ * For PHP 5.x, depends on https://github.com/paragonie/random_compat
+ *
+ * @param int $length How many characters do we want?
+ * @param string $keyspace A string of all possible characters
+ * to select from
+ * @return string
+ */
+function random_secure_password(
+ $minlen = 0,
+ $maxlen = 10,
+ $keyspace = '!#%&+-.0123456789=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz')
+{
+ $generated_pw = '';
+ $max = mb_strlen($keyspace, '8bit') - 1;
+ if ($max < 1) {
+ throw new Exception('$keyspace must be at least two characters long');
+ }
+ for ($i = 0; $i < $maxlen; ++$i) {
+ $generated_pw .= $keyspace[random_int($minlen, $max)];
+ }
+ return $generated_pw;
+}
+
/** PHP5 deprecated old-style globals if !(bool)ini_get('register_long_arrays').
* See Bug #1180115
* We want to work with those old ones instead of the new superglobals,
@@ -77,62 +151,165 @@
$posted = $GLOBALS['HTTP_POST_VARS'];
if (!empty($posted['create'])) {
- $new_password = random_good_password();
- echo "<p>The newly created random password is:<br />\n<br /> \n<samp><strong>",
- htmlentities($new_password), "</strong></samp></p>\n";
+ //$new_password = random_good_password();
+ $new_password = random_secure_password();
+ echo "<h2>Randomly generated password</h2>\n";
+ echo "<p>The newly generated unencrypted random password is: <samp>",
+ htmlentities($new_password), "</samp></p>\n";
+?>
+ <input type="hidden" value="<?php echo htmlentities($new_password,
+ ENT_QUOTES, 'UTF-8'); ?>" id="generated_pw_text">
+ <button onclick="copyToClipboard_pw()">Copy generated password to clipboard</button>
+
+ <script>
+ function copyToClipboard_pw() {
+ var copyText_pw = document.getElementById("generated_pw_text");
+ copyText_pw.select();
+ navigator.clipboard.writeText(copyText_pw.value)
+ alert("Copied the unencryped password:\n" + copyText_pw.value);
+ }
+ </script>
+ <hr />
+<?php
$posted['password'] = $new_password;
$posted['password2'] = $new_password;
}
+ if ( ($posted) && ($posted['password'] != "")
+ && ($posted['password'] == $posted['password2'])
+ )
+{
+ $password = $posted['password'];
+ $password2 = $posted['password2'];
-if (($posted['password'] != "")
- && ($posted['password'] == $posted['password2'])
-) {
- $password = $posted['password'];
- /**
- * https://www.php.net/manual/en/function.crypt.php
- */
- // Use the maximum salt length the system can handle.
- $salt_length = max(
- CRYPT_SALT_LENGTH,
- 2 * CRYPT_STD_DES,
- 9 * CRYPT_EXT_DES,
- 12 * CRYPT_MD5,
- 16 * CRYPT_BLOWFISH
- );
- // Generate the encrypted password.
- $encrypted_password = crypt($password, rand_ascii($salt_length));
- $debug = $HTTP_GET_VARS['debug'];
- if ($debug) {
- echo "The password was encrypted using a salt length of: $salt_length<br />\n";
+ if (!function_exists('password_hash')) {
+ /**
+ * https://www.php.net/manual/en/function.crypt.php
+ */
+ // Use the maximum salt length the system can handle.
+ $salt_length = max(
+ CRYPT_SALT_LENGTH,
+ 2 * CRYPT_STD_DES, // 2 character salt
+ 9 * CRYPT_EXT_DES,
+ 12 * CRYPT_MD5,
+ 16 * CRYPT_BLOWFISH
+ // There are others but I don't know their lengths
+ );
+ // Generate the encrypted password.
+ $randascii=rand_ascii($salt_length);
+ $encrypted_password = crypt($password, $randascii);
+ $debug=0;
+ if (isset($HTTP_GET_VARS['debug'])) {
+ $debug = $HTTP_GET_VARS['debug'];
+ }
+ if ($debug) {
+ echo "<p>\$randascii= <samp>".htmlentities($randascii, ENT_QUOTES,
+ 'UTF-8')."</samp></p>\n";
+ echo "<p>The password was encrypted using a salt length of: $salt_length</p>\n";
+ echo "<p>\$randascii strlen is <samp>".strlen($randascii)."</samp></pre>\n";
+ }
+ } else {
+ /**
+ * https://www.php.net/manual/en/function.password-hash.php
+ */
+ echo "<h2>Encryption results</h2>\n";
+ $encrypted_password = password_hash($password, PASSWORD_DEFAULT);
+ echo "<p>The password was encrypted using the newer password_hash() function instead of crypt().</p>\n";
}
- echo "<p>The encrypted password is:<br />\n<br /> \n<samp><strong>",
- htmlentities($encrypted_password), "</strong></samp></p>\n";
- echo "<hr />\n";
-} elseif ($posted['password'] != "") {
- echo "The passwords did not match. Please try again.<br />\n";
+ if ($encrypted_password === '*0' || $encrypted_password === '*1') {
+ echo "<p>Error: crypt() function failed. Just try again!</p>";
+ } else {
+ $admin_ini_entry = 'ADMIN_PASSWD="'.$encrypted_password.'"';
+ echo "<p>The encrypted password is: <samp>",
+ htmlentities($encrypted_password)."</samp></p>\n";
+
+ ?>
+ <input type="hidden" value="<?php echo htmlentities($encrypted_password,
+ ENT_QUOTES, 'UTF-8'); ?>" id="encrypted_password_text">
+ <button onclick="copyToClipboard_encrpw()">Copy encrypted password to clipboard</button>
+ <script>
+ function copyToClipboard_encrpw() {
+ var copyText_encrpw = document.getElementById("encrypted_password_text");
+ copyText_encrpw.select();
+ navigator.clipboard.writeText(copyText_encrpw.value)
+ alert("Copied the encrypted password:\n" + copyText_encrpw.value);
+ }
+ </script>
+<?php
+ echo "<hr />\n";
+ echo "<p>Copy this into your <span class=\"filename\">phpwiki/config/config.ini</span> file: <samp>".
+ htmlentities($admin_ini_entry, ENT_QUOTES, 'UTF-8'). "</samp></p>\n";
+?>
+ <input type="hidden" value="<?php echo htmlentities($admin_ini_entry,
+ ENT_QUOTES, 'UTF-8'); ?>" id="admin_ini_entry_text">
+ <button onclick="copyToClipboard_ini()">Copy config.ini entry to clipboard</button>
+ <script>
+ function copyToClipboard_ini() {
+ var copyText_ini = document.getElementById("admin_ini_entry_text");
+ copyText_ini.select();
+ navigator.clipboard.writeText(copyText_ini.value)
+ alert("Copied the config.ini entry:\n" + copyText_ini.value);
+ }
+ </script>
+<?php
+ echo "<hr />\n";
+ }
+
+} elseif (($posted) &&
+ ($posted['password'] != "")
+ )
+{
+ echo "<p>The passwords did not match. Please try again.</p>\n";
}
if (empty($REQUEST_URI)) {
- $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
+ if (!empty($HTTP_ENV_VARS)) {
+ $REQUEST_URI = $HTTP_ENV_VARS['REQUEST_URI'];
+ }
}
if (empty($REQUEST_URI)) {
$REQUEST_URI = $_SERVER['REQUEST_URI'];
}
+
+if (($posted) &&
+ ($posted['password'] != "")
+ ) {
+ $password=$posted['password'];
+ } else {
+ $password="";
+ }
+if (($posted) &&
+ ($posted['password2'] != "")
+ ) {
+ $password2=$posted['password2'];
+ } else {
+ $password2="";
+ }
?>
-
-<form action="<?php echo $REQUEST_URI ?>" method="post">
+<form action="<?php echo $REQUEST_URI ?>" method="post" id="myForm">
+<h2>Encrypt a password or generate one at random</h2>
<fieldset>
<legend>Encrypt</legend>
- Enter a password twice to encrypt it:<br/>
- <input type="password" name="password" value=""/><br/>
- <input type="password" name="password2" value=""/> <input type="submit" value="Encrypt"/>
- </fieldset>
- <br/>
- or:<br/>
- <br/>
+ <p>Enter a password twice to encrypt it:</p>
+ <input type="password" name="password" value="<?php
+ echo htmlentities($password, ENT_QUOTES, 'UTF-8'); ?>"/><br/>
+ <input type="password" name="password2" value="<?php
+ echo htmlentities($password2, ENT_QUOTES, 'UTF-8'); ?>"/>
+ <input type="submit" value="Encrypt"/>
+ <button type="button" onclick="clearform()">Clear</button>
+ </fieldset>
+ <script>
+ function clearform() {
+ document.getElementsByName('password')[0].value = '';
+ document.getElementsByName('password2')[0].value = '';
+ }
+ </script>
+ <p>or:</p>
<fieldset>
<legend>Generate</legend>
- Create a new random password: <input type="submit" name="create" value="Create"/>
+ <p>Randomly generate a new password:
+ <input type="submit" name="create" value="Generate"/><br />
+ (Just keep clicking Generate until you see a password you like.)</p>
</fieldset>
+ <script>
</form>
</body>
</html>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|