- 论坛徽章:
- 0
|
我写的密码强度验证方法
Javascript代码- /*
- *说明:
- * 该方法主要分析密码的内容构成,密码长度等情况,然后评分划分密码强度等级
- *参数:
- * 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--;
- }
- // 不能由连续的字母组成,例如:abcdefg
- if ("abcdefghijklmnopqrstuvwxyz".indexOf(pwd) > 0) {
- level--;
- }
- // 纯字母组成的密码不能是键盘上的相邻键位字母组合,例如:qwertyu
- if ("qwertyuiop".indexOf(pwd) > 0 || "asdfghjkl".indexOf(pwd) > 0
- || "zxcvbnm".indexOf(pwd) > 0) {
- level--;
- }
- // 不能是2段短字符的重复,例如:567567
- if (pwd.length % 2 == 0) {
- var part1 = pwd.substring(0, pwd.length / 2);
- var part2 = pwd.substring(pwd.length / 2);
- if (part1 == part2)
- level--;
- }
- // 不能是3段短字符的重复,例如:121212
- if (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--;
- }
- // 不能是一个日期,例如:19870723
- if (/^[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]);
- }
复制代码 |
|