Posted By

Wardy on 11/13/08


Tagged

readable password


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

luman


Create easily readable password in PHP


 / Published in: PHP
 

URL: http://www.sitepoint.com/blogs/2008/11/13/how-to-create-friendlier-random-passwords/

Now, it’s true that if we use this function to generate passwords, the passwords created will be less secure — this function can only create 285 (17 million) different five-character passwords whereas if I had included the missing characters this number would’ve been 365 (60 million). That’s a trade-off you’ll have to accept in return for fewer frustrated users having difficulty logging on to your site, which can be mitigated by forcing users to change their generated password to something chosen by them.

  1. function GenerateRandomString($length) {
  2. $characters = array('2', '3', '4', '5', '6', '7', '8', '9',
  3. 'a', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'p',
  4. 'q', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
  5. $randomString = "";
  6. while(strlen($randomString) < $length) {
  7. $randomCharacterIndex = rand(0, count($characters));
  8. $randomString .= $characters[$randomCharacterIndex];
  9. }
  10. return $randomString;
  11. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MMDeveloper on November 13, 2008

in a way that's similar to a password generator I wrote a while back

http://mmdeveloper.selfip.com/Random+Password+Generator-p76.html

class passwordGenerator { var $input = NULL; var $charset = NULL;

var $misc = NULL;

function __construct($input) {
    $this->passwordGenerator($input);
}

function passwordGenerator($input) {
    $this->input = $input;

    $this->charset = array(
            // lower cased letters
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",

            // upper cased letters
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",

            // numbers
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",

            // other non-special keyboard characters
            "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "_", "+",
            "[", "]", "{", "}", "|", ";", ":", ",", ".", "/", "", "?",

            //questionable characters
            "\\"
        );

    shuffle($this->charset);
    $this->misc = array (
            "charsetLength" => count($this->charset)
        );
}

function generateByChars() {
    srand((float) microtime() * 10000000);
    $result = "";
    $this->input["NumChars"] = intval($this->input["NumChars"]);


    if ($this->input["NumChars"] > 0) {
        for ($i = 0; $i < $this->input["NumChars"]; ++$i){
            $result .= $this->charset [rand (0, $this->misc["charsetLength"])];
        }
    }
    else {
        $result = "Error, there is a problem with your character count input.";
    }

    return $result;
}

function generateByPhrase() {
    $result = "";
    $this->input["Phrase"] = trim($this->input["Phrase"]);



    if ($this->input["Phrase"] != "") {
        $InputArray = preg_split ("/[\s]+/", $this->input["Phrase"]);

        foreach ($InputArray as $key){
            if (is_numeric ($key) && strlen ($key) >= 3){
                if (strlen ($key) == 4){
                    $result .= $key[2];
                    $result .= $key[3];
                }
                else {
                    $result .= $key[0];
                    $result .= $key[(strlen ($key) - 1)];
                }
            }
            else {
                $result .= $key[0];
            }
        }
    }
    else {
        $result = "Error, there is a problem with your phrase input.";
    }

    return $result;
}

}

You need to login to post a comment.