首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

小弟我写的密码强度验证方法(原创)

2012-08-26 
我写的密码强度验证方法(原创)/* *说明: *该方法主要分析密码的内容构成,密码长度等情况,然后评分划分密码

我写的密码强度验证方法(原创)

/* *说明: *  该方法主要分析密码的内容构成,密码长度等情况,然后评分划分密码强度等级 *参数: *  pwd 密码,字符串类型 *  obj 密码强度显示的地方,必须是jquery封装对象  *  minLen 密码最小长度 *  maxLen 密码最大长度 */function showPassstrength(pwd, obj, minLen, maxLen) {var dictionary = [];var containsAToZ = function(str) {var aToZ = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z" ];var r = false;for ( var i = 0; i < aToZ.length; i++) {if (str.indexOf(aToZ[i]) >= 0) {r = true;break;}}return r;}var containsNumber = function(str) {var numbers = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ];var r = false;for ( var i = 0; i < numbers.length; i++) {if (str.indexOf(numbers[i]) >= 0) {r = true;break;}}return r;}var contains_ = function(str) {return str.indexOf("_") >= 0 ? true : false;}var isANumberSequence = function(str) {var array = str.split("");var eq = true;var num = parseInt(array[1]);var diff = num - parseInt(array[0])for ( var i = 2; i < array.length; i++) {if (parseInt(array[i]) != num + diff) {eq = false;break;}num = array[i];}return eq;}var reverse = function(str) {return str.split("").reverse().join("");}pwd = pwd.toLowerCase();var level = 1;/** ****************加分因子***************** */if (containsAToZ(pwd)) {level++;}if (containsNumber(pwd)) {level++;}if (contains_(pwd)) {level++;}if ((containsAToZ(pwd) && containsNumber(pwd))|| (containsAToZ(pwd) && contains_(pwd))|| (containsNumber(pwd) && contains_(pwd))) {level++;}if (containsAToZ(pwd) && containsNumber(pwd) && contains_(pwd)) {level++;}/** ****************减分因子***************** */if (pwd.length < (minLen + maxLen) / 2 && pwd.length >= minLen) {level--;}var pwdArray = pwd.split("");// 全部由同一个字符组成的直接判为弱var allEquals = true;var element = pwdArray[0];for ( var i = 1; i < pwdArray.length; i++) {if (pwdArray[i] != element) {allEquals = false;break;}element = pwdArray[i];}if (allEquals)level = 0;// 纯数字的密码不能是一个等差数列数列if (/^[0-9]+$/.test(pwd)&& (isANumberSequence(pwd) || isANumberSequence(reverse(pwd)))) {level--;}// 不能由连续的字母组成,例如:abcdefgif ("abcdefghijklmnopqrstuvwxyz".indexOf(pwd) > 0) {level--;}// 纯字母组成的密码不能是键盘上的相邻键位字母组合,例如:qwertyuif ("qwertyuiop".indexOf(pwd) > 0 || "asdfghjkl".indexOf(pwd) > 0|| "zxcvbnm".indexOf(pwd) > 0) {level--;}// 不能是2段短字符的重复,例如:567567if (pwd.length % 2 == 0) {var part1 = pwd.substring(0, pwd.length / 2);var part2 = pwd.substring(pwd.length / 2);if (part1 == part2)level--;}// 不能是3段短字符的重复,例如:121212if (pwd.length % 3 == 0) {var part1 = pwd.substring(0, pwd.length / 3);var part2 = pwd.substring(pwd.length / 3, pwd.length / 3 * 2);var part3 = pwd.substring(pwd.length / 3 * 2);if (part1 == part2 && part2 == part3)level--;}// 不能是一个日期,例如:19870723if (/^[0-9]+$/.test(pwd)) {if (pwd.length == 8) {var year = parseInt(pwd.substring(0, 4));var month = parseInt(pwd.substring(4, 6));var day = parseInt(pwd.substring(5, 7));if (year >= 1000 && year < 2100 && month >= 1 && month <= 12&& day >= 1 && day <= 31) {level--;}}}// 不能位于内置字典内for ( var i = 0; i < dictionary.length; i++) {if (pwd == dictionary[i] || dictionary[i].indexOf(pwd) >= 0) {level--;break;}}if (level < 0)level = 0;if (level > 5)level = 5;var levelNames = [ "很弱", "较弱", "一般", "较好", "良好", "优秀" ];obj.html(levelNames[level]);}

热点排行