Here is the code which you can use to generate random and secure password in PHP.
<?
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.
In JavaScript To generate password use the following function.
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.
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.
var err = checkPassword(pw,12,8);
if (errors){
//Display error message.
alert('The following errors occered\n' + err);
return;}
else{
//password is OK.
}
<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>
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.