密码强度检测(收藏)

密码强度提示是最近流行的一个功能,但是99%的案例都忽略了检测机制。

基本上都是把字符分成四类:数字、小写英文、大写英文、符号,然后按照组合复杂度来直接判断强弱程度:

单一,是弱密码。
两两组合,是中密码。
超过两种组合,是强密码。
方案一


<SCRIPT language=javascript>

//CharMode函数
//测试某个字符是属于哪一类.
function CharMode(iN)
{
if (iN>=48 && iN <=57) //数字
return 1;
if (iN>=65 && iN <=90) //大写字母
return 2;
if (iN>=97 && iN <=122) //小写
return 4;
else
return 8; //特殊字符
}

//bitTotal函数
//计算出当前密码当中一共有多少种模式
function bitTotal(num)
{
modes=0;
for (i=0;i<4;i++)
{
if (num & 1) modes++;
num>>=1;
}
return modes;
}

//checkStrong函数
//返回密码的强度级别

function checkStrong(sPW)
{
if (sPW.length<=4)
return 0; //密码太短
Modes=0;
for (i=0;i<sPW.length;i++)
{
//测试每一个字符的类别并统计一共有多少种模式.
Modes|=CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}

//pwStrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色

function pwStrength(pwd)
{
O_color="#eeeeee";
L_color="#FF0000";
M_color="#FF9900";
H_color="#33CC00";
if (pwd==null||pwd=="")
{
Lcolor=Mcolor=Hcolor=O_color;
}
else
{
S_level=checkStrong(pwd);
switch(S_level)
{
case 0:
Lcolor=Mcolor=Hcolor=O_color;
case 1:
Lcolor=L_color;
Mcolor=Hcolor=O_color;
break;
case 2:
Lcolor=Mcolor=M_color;
Hcolor=O_color;
break;
default:
Lcolor=Mcolor=Hcolor=H_color;
}
}
document.getElementById("strength_L").style.background=Lcolor;
document.getElementById("strength_M").style.background=Mcolor;
document.getElementById("strength_H").style.background=Hcolor;
return;
}

</SCRIPT>

<FORM name="form1" action=""><P>
输入密码:</P><P>
<INPUT onblur=pwStrength(this.value) onkeyup=pwStrength(this.value) type=password size=10 value="">
<BR> 密码强度: </P><P>
<TABLE style="DISPLAY: inline" borderColor=#cccccc height=23 cellSpacing=0 cellPadding=1 width=217 border=1>
<TBODY>
<TR align=middle bgColor=#eeeeee>
<TD id=strength_L width="33%">弱</TD>
<TD id=strength_M width="33%">中</TD>
<TD id=strength_H width="33%">强</TD>
</TR>
</TBODY>
</TABLE>
</P><P> </P><P> </P></FORM>
<PRE></PRE>

方案二

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>密码</title>
<script type="text/javascript">
function chkpwd(obj){
var t=obj.value;
var id=getResult(t);

//定义对应的消息提示
var msg=new Array(4);
msg[0]="密码过短";
msg[1]="密码强度差";
msg[2]="密码强度良好";
msg[3]="密码强度高";

var sty=new Array(4);
sty[0]=-45;
sty[1]=-30;
sty[2]=-15;
sty[3]=0;

var col=new Array(4);
col[0]="gray";
col[1]="red";
col[2]="#ff6600";
col[3]="Green";

//设置显示效果
var bImg="http://bbs.blueidea.com/attachments/2006/12/7/pwdlen_dSIPeEGQWxfO.gif";//一张显示用的图片
var sWidth=300;
var sHeight=15;
var Bobj=document.getElementById("chkResult");

Bobj.style.fontSize="12px";
Bobj.style.color=col[id];
Bobj.style.width=sWidth + "px";
Bobj.style.height=sHeight + "px";
Bobj.style.lineHeight=sHeight + "px";
Bobj.style.background="url(" + bImg + ") no-repeat left " + sty[id] + "px";
Bobj.style.textIndent="20px";
Bobj.innerHTML="检测提示:" + msg[id];
}

//定义检测函数,返回0/1/2/3分别代表无效/差/一般/强
function getResult(s){
if(s.length < 4){
return 0;
}
var ls = 0;
if (s.match(/[a-z]/ig)){
ls++;
}
if (s.match(/[0-9]/ig)){
ls++;
}
if (s.match(/(.[^a-z0-9])/ig)){
ls++;
}
if (s.length < 6 && ls > 0){
ls--;
}
return ls
}
</script>
<style type="text/css">
body{
font-size:12px;
font-family: Arial, Helvetica, sans-serif;
margin:0;
}
form{
margin:2em;
}
#chkResult{margin-left:53px;height:15px;}
</style>

</head>

<body>
<form name="form1">
<label for="pwd">用户密码</label>
<input type="password" name="pwd" onblur="chkpwd(this)" />
<div id="chkResult"></div>
<label for="pwd2">重复密码</label>
<input type="password" name="pwd2" />
</form>

</body>

</html>

方案三
<html>
<body>
<form id="myform">
<h1>Password Strength Meter - Demo</h1>
<script type="text/javascript" s>
// Password strength meter v1.0
// Matthew R. Miller - 2007
// www.codeandcoffee.com
// Based off of code from http://www.intelligent-web.co.uk

// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
var bCheckNumbers = true;
var bCheckUpperCase = true;
var bCheckLowerCase = true;
var bCheckPunctuation = true;
var nPasswordLifetime = 365;

// Check password
function checkPassword(strPassword)
{
// Reset combination count
nCombinations = 0;

// Check numbers
if (bCheckNumbers)
{
strCheck = "0123456789";
if (doesContain(strPassword, strCheck) > 0)
{
nCombinations += strCheck.length;
}
}

// Check upper case
if (bCheckUpperCase)
{
strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (doesContain(strPassword, strCheck) > 0)
{
nCombinations += strCheck.length;
}
}

// Check lower case
if (bCheckLowerCase)
{
strCheck = "abcdefghijklmnopqrstuvwxyz";
if (doesContain(strPassword, strCheck) > 0)
{
nCombinations += strCheck.length;
}
}

// Check punctuation
if (bCheckPunctuation)
{
strCheck = ";:-_=+\|//?^&!.@$?*()%~<>{}[]";
if (doesContain(strPassword, strCheck) > 0)
{
nCombinations += strCheck.length;
}
}

// Calculate
// -- 500 tries per second => minutes
var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;

// Number of days out of password lifetime setting
var nPerc = nDays / nPasswordLifetime;

return nPerc;
}

// Runs password through check and then updates GUI
function runPassword(strPassword, strFieldID)
{
// Check password
nPerc = checkPassword(strPassword);

// Get controls
var ctlBar = document.getElementById(strFieldID + "_bar");
var ctlText = document.getElementById(strFieldID + "_text");
if (!ctlBar || !ctlText)
return;

// Set new width
var nRound = Math.round(nPerc * 100);
if (nRound < (strPassword.length * 5))
{
nRound += strPassword.length * 5;
}
if (nRound > 100)
nRound = 100;
ctlBar.style.width = nRound + "%";

// Color and text
if (nRound > 95)
{
strText = "极佳";
strColor = "#3bce08";
}
else if (nRound > 75)
{
strText = "很好";
strColor = "orange";
}
else if (nRound > 50)
{
strText = "一般";
strColor = "#ffd801";
}
else
{
strColor = "red";
strText = "极差";
}
ctlBar.style.backgroundColor = strColor;
ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}

// Checks a string for a list of characters
function doesContain(strPassword, strCheck)
{
nCount = 0;

for (i = 0; i < strPassword.length; i++)
{
if (strCheck.indexOf(strPassword.charAt(i)) > -1)
{
nCount++;
}
}

return nCount;
}


</script>

<b>Password:</b>
<br />
<input type="password" id="mypassword" name="mypassword" onkeyup="runPassword(this.value, 'mypassword');" />
<br />
<br />
<b>Password Strength:</b>
<br />
<div style="width: 100px;">
<div id="mypassword_text" style="font-size: 10px;"></div>
<div id="mypassword_bar" style="font-size: 1px; height: 2px; width: 0px; border: 1px solid white;"></div>
</div>
</form>
</body>
</html>


方案四
<html>
<title>密码强度计算-www.codefans.net</title>
<head>
<style type="text/css">
<!--
.text {
height: 16px;
width: 160px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #999999;
border-right-color: #F0F0F0;
border-bottom-color: #F0F0F0;
border-left-color: #999999;
}
.style1 {font-size: 12px}
.m {
background-image: url(image/m.gif);
background-repeat: no-repeat;
height: 15px;
width: 158px;
}
.r {
background-image: url(image/r.gif);
background-repeat: no-repeat;
height: 15px;
width: 158px;
}
.z {
background-image: url(image/z.gif);
background-repeat: no-repeat;
height: 15px;
width: 158px;
}
.q {
background-image: url(image/q.gif);
background-repeat: no-repeat;
height: 15px;
width: 158px;
}
.style5 {font-size: 12px; color: #FF0000; }
.style6 {font-size: 12px; color: #F1F1F1; }
.style7 {font-size: 12px; color: #F1F1F1; }
.style8 {font-size: 12px; color: green; }
.style9 {font-size: 12px; color: red; }
-->
</style>
</head>
<script language=javascript>
function CharMode(iN){
if (iN>=48 && iN <=57) //数字
return 1;
if (iN>=65 && iN <=90) //大写字母
return 2;
if (iN>=97 && iN <=122) //小写
return 4;
else
return 8; //特殊字符
}
function bitTotal(num){
modes=0;
for (i=0;i<5;i++){
if (num & 1) modes++;
num>>>=1;
}
return modes;
}
function checkStrong(sPW){
if (sPW.length<=5)
return 0; //密码太短
Modes=0;
for (i=0;i<sPW.length;i++){
Modes|=CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}
function pwStrength(pwd,vImg,vImgs,ilength){
if(pwd.length<=0){
document.getElementById(vImgs).innerHTML="<span style=color:red;font-size:12px>"+16+"</span>";
}
else{
document.getElementById(vImgs).innerHTML="<span style=color:red;font-size:12px>"+(16-pwd.length)+"</span>";
}
if (pwd==null||pwd==''){
document.getElementById(vImg).className="m";
//document.getElementById(ilength).className="style6";
}
else{
S_level=checkStrong(pwd);
switch(S_level){
case 0:
document.getElementById(vImg).className="m";
//document.getElementById(ilength).className="style6";
break;
case 1:
document.getElementById(vImg).className="r";
//document.getElementById(ilength).className="style7";
break;
case 2:
document.getElementById(vImg).className="z";
//document.getElementById(ilength).className="style8";
break;
default:
document.getElementById(vImg).className="q";
//document.getElementById(ilength).className="style9";
}
}
return;
}
</script>
<body>
<div align="center">
<p> <br>
</p>
<form name="form1" method="post" action="">
<table width="240" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="69" height="24">
<div align="right" class="style1">密    码:</div></td>
<td width="171"><div align="center">
<input name="str1" type="password" class="text" onKeyUp="pwStrength(this.value,'img','imgs','ilength')" value="" maxlength="16" >
</div></td>
</tr>
<tr>
<td height="24">
<div align="right" class="style1">密码强度:</div></td>
<td height="19"><div align="center">
<table width="160" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle" class="m" id="img">
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="46%"><div align="right" class="style6" id="ilength"></div></td>
<td width="14%"> </td>
<td width="40%" id="imgs"><span class="style5">16</span></td>
</tr>
</table></td>
</tr>
</table>
</div></td>
</tr>
</table>
</form>
</div>
</body>
</html>

from:http://itlife365.com/blog/post/password-test.php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值