Archive for February, 2008

A random Password Generator script - Full featured

Here is random Password generator function, it can produce complex passwords of any desired length. It can generate passwords which are alpha-numeric + with special characters (optional) + upper and lower case mixed within it.

 function generateCode($characters,$special_char=false) {
        $possible = '1234567890abcdefghijklmnopqrstuvwxyz';
            if($special_char)
              $possible .= '!@#$%^&*()_+-=[]{};\':",./\\<>?|`~';
        $code = '';
        $i = 0;
        while ($i < $characters) {
         $code1 = substr($possible, mt_rand(0, strlen($possible)-1), 1);
         if(rand()%2==0)
         $code1= strtoupper($code1);
         $code .=$code1;
         $i++;
         }

        return $code;
 }
 

Features of this function:

  • Alpha-numeric passwords
  • You can get random password of any desired length
  • Can produce password of combined upper and lowercase characters.
  • Can optionally produce passwords with special characters

Suppose you want to produce password of length 8 characters, then the syntax to call the function is

generateCode(8);
 

If you want to create password with special characters also, then the syntax will be

generateCode(8,true);
 

Some examples of generated passwords are:
Mm69lIGf
“)5E%\{7
M4p3OVEod6Dq9cJZMkGZMEnm31JVGcNY8iK
J4j:8~-72)^fF>=6)S<(”{~->a0gQ6(DrEk



If you don’t need a complex passwords generator like this then you can try A simple Password Generator here it is one-line code which can generate simple random passwords.

A simple Password Generator

There are many big and complex Password generator scripts available on the Internet, but often we don’t need to go into such complexities.

Here is a simple one-line password generator code in PHP,

       substr(md5(rand()),0,8);
 

First, a random number is taken, then the MD5 hashing is done on it, then a substring is taken from that. That substring is your generated password.

In the above code just change the the last number (which is ‘8′ here), to get your desired length of password.

However this has two small limitations:

  • Though the generated password will be alpha-numeric, the characters will not go beyond ‘F’ , since they are hex codes.
  • Password length cannot be more than 32 characters, since a MD5 hash is taken. However you can always concatenate this code one after another to get any desired length of random password.

Anyway, these are very simple limitations, this will not bother you in ordinary cases.

Some examples of generated password are:
861cf
3ed0d10f
33dc56970486a86b15cc



If you need a password generator without these limitations then you can try A Full featured Password Generator here, this complex password generator can produce alpha-numeric passwords of any length.