Random password generator

Random password generator with PHP

Here is the code which you can use to generate random and secure password in PHP.

Code | Download
<?
function generate_password($length='')
{
         $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_?/:(){}[]0123456789';
         $max=strlen($str);
         $length=@round($length);
         if(empty($length)){$length=rand(8,12);}
         $password='';
         for($i=0; $i<$length; $i++){$password.=$str{rand(0,$max-1)};}
         return $password;
}
?>

In the above code the PHP function generate_password() takes one optional argument, the length of the password. If you do not specify the length then this function will return a random password of length from eight to twelve characters. PHP variavle $str hold the characters to be used to generate password. You can edit this string according to your need.

Random password generator with JavaScript

In JavaScript To generate password use the following function.

Code | Download
function generatePW()
{
var i=0;
var s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_?/:(){}[]0123456789";
var pw="";
var c=(Math.random() * 3)+9;
for (i=0; i<c; i++)
{
pw+=s.charAt(Math.random() * 72);
}
return(pw);}

Here the JavaScript function Math.random() generates a random number from 0 to 1 (actually just less than 1). So the code:
var c=(Math.random() * 3)+9; will generate a number between 9 to just less than 12.

User comments:

How to validate password

COMMENT BY: Mouli DATE: Oct 05, 22:24

Thank you! You often write very interesting articles. In truth, immediately i didn't understand the code. But after re-reading all at once became clear. I've a little question to you. How can validate password as it has been typed according to the generation rule?

Generation rule:
Password length: 8 to 12.
Character contain: defined by var s.

Validate password with JavaScript

COMMENT BY: admin DATE: Oct 31, 20:50
Here is the function by which you can validate password. Here Cmax and Cmin is the maximum and minimum length of password.
Code:
function checkPassword(str,Cmax,Cmin){
var errors='';
var s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var c=str.length;
if ((c<Cmin)||(c>Cmax)){
errors+='Put password between ' + Cmin + ' to ' + Cmax' characters.\n';}
var i;
for (i=0; i<c; i++)
{
if(s.indexOf(str.charAt(i))==-1){errors+='\"'+ str.charAt(i)+'\" not allowed in password field.\n';}
}
return errors;
}
To use this function pass the password typed by the user; verify the its return value.
Code:
var err = checkPassword(pw,12,8);
if (errors){
//Display error message.
alert('The following errors occered\n' + err);
return;}
else{
//password is OK.
}

Password strength

COMMENT BY: newbee DATE: Feb 06, 12:29
How can i measure the strength of my password (like CPanel)?
Is there any way to generate 100% strong password every time when I run the function?
Thanks in advance.

Generate CPanel password

COMMENT BY: admin DATE: Feb 13, 02:08
Actually I don't know the algorithm that CPanel follows to measure the strength of a password. But I can tell you that there is no such password, which is 100% strong. If you want to improve the security level of your password then,
1. Do not generate password less than 8 characters.
2. Mix small, capital, digit and special characters in random fashion and ensure that the generated password contains at least one occurrence of every type of character.
How to generate strong password:

1. Generate password in conventional way. Length of this password should be 4 less than the desired length.
2. Randomly select one character from each group.
3. Insert those random characters at random places of the generated password.
The script code will be as follows:
Code:
<script type="text/javascript">
<!--
function CPanelPW()
{
var i=0;
var sSma="abcdefghijklmnopqrstuvwxyz";
var sCap="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sDig="0123456789";
var sSpe="-_?/:(){}[]";
var s=sSma + sCap + sDig + sSpe;
//Generate random word.
var pw=randomX(0,s);
//Generate one random character from each group.
var rSma=randomX(1,sSma);
var rCap=randomX(1,sCap);
var rDig=randomX(1,sDig);
var rSpe=randomX(1,sSpe);
//Select a random and unique position of pw and insert random characters.
var pos=unique(0,pw);
pw=pw.substr(0,pos)+rSma+pw.substr(pos);
pos=unique(pos+1,pw);
pw=pw.substr(0,pos)+rCap+pw.substr(pos);
pos=unique(pos+1,pw);
pw=pw.substr(0,pos)+rDig+pw.substr(pos);
pos=unique(pos+1,pw);
pw=pw.substr(0,pos)+rSpe+pw.substr(pos);
//For experiment:
document.getElementById('testing').value=pw;
return pw;}

function randomX(c,s)
{
var p = '';
if(c==0){c=(Math.random() * 3)+9;}
for (var i=0; i<c; i++){p+=s.charAt(Math.random() * (s.length-1));}
return(p);}

function unique(c,pw)
{
var pos = c;
do{pos= Math.random()*(pw.length -1);}while(pos == c);
return pos;
}
-->
</script>

Add comment

RAMUI WEBBLOG
HomeAboutContact SitemapTerms of useXMLForum
© 2010,  http://www.ramui.com   All rights reserved.
Powered by: ramui webblog® Version 1.0