本文实例讲述了js密码生成与强度检测的方法。分享给大家供大家参考,具体如下:
1. 生成强密码
截图如下:
相关代码如下:
function getpwd(n){ var s = ''; while(n--) s += string.fromcharcode(33 + math.floor(math.random()*(126-33))) document.getelementbyid('txt1').value = s;}
2. 计算密码破解时间
截图如下:
相关代码如下:
function gettime(){ var str = '预计破解用时:'; var selchar = document.getelementbyid('selchar'); var txtpwdlen = document.getelementbyid('txtpwdlen'); var num = math.pow(parseint(selchar.value), parseint(txtpwdlen.value)); str += formattime(num / (1024*1024*1024*2.4*2)); document.getelementbyid('span2').innerhtml = str;}function formattime(s){ var str = ''; if(s= 1) str = s % 60 + '秒' + str; s = math.floor(s / 60); if(s >= 1) str = s % 60 + '分' + str; s = math.floor(s / 60); if(s >= 1) str = s % 24 + '时' + str; s = math.floor(s / 24); if(s >= 1) str = s + '天' + str; return str;}
3. 密码安全检测
截图如下:
相关代码如下:
function showpwd(){ var p = document.getelementbyid('txt2').value; if(p.length < 4) { showerror('密码至少4位!'); return; } var o = checkpwd(p); if(o.issame) { showerror('密码为重复字符!'); return; } for(var i=0; i1900 && year0 && month0 && day<32) { showerror('不要使用日期作为密码!'); return; } } } var hasupper = false; var haslow = false; var hasnum = false; var hasother = false; for(var i=0; i=65&&c=97&&c=48&&c<=57)hasnum=true; else hasother=true; } var pwdnum = 0; if(hasupper)pwdnum+=26; if(haslow)pwdnum+=26; if(hasnum)pwdnum+=10; if(hasother)pwdnum+=32; var num = math.pow(pwdnum, p.length); var str = '密码长度:' + p.length + ' 强度:' + pwdnum + ' 预计破解用时:' + formattime(num / (1024*1024*1024*2.4*2)); var span1 = document.getelementbyid('span1'); span1.style.color = 'blue'; span1.innerhtml = str;}
4. 检测键盘是否大写锁定(caps lock键状态)
截图如下:
相关代码如下:
var $lock = false;function checkcapslock(fn){ document.documentelement.onkeypress = function(e) { var e = e || event; var k = e.keycode || e.which; var s = e.shiftkey || (k == 16) || false; if(k>=65&&k=97&&k<=122)$lock=s; fn($lock); } document.documentelement.onkeyup = function(e) { var e = e || event; var k = e.keycode || e.which; if(k==20)$lock = !$lock; fn($lock); }}