
var PasswordUtil = (function () {
    var f = {
        charMode: function (iN) {
            if (iN >= 48 && iN <= 57) {
                //数字
                return 1;
            }
            if (iN >= 65 && iN <= 90) {
                //大写
                return 2;
            }
            if (iN >= 97 && iN <= 122) {
                //小写
                return 4;
            }
            return 8;
        },
        bitTotal: function (num) {
            var modes = 0;
            for (i = 0; i < 4; i++) {
                if (num & 1) modes++;
                num >>>= 1;
            }
            return modes;
        },
        checkStrong: function (sPW) {
            if (sPW.length < 6) {
                return 0; //密码太短，不检测级别
            }
            var Modes = 0;
            for (var i = 0; i < sPW.length; i++) {
                //密码模式
                Modes |= f.charMode(sPW.charCodeAt(i));
            }
            return f.bitTotal(Modes);
        },
        /**
         * 判断密码强度，判断安全分数
         * @param pwd
         */
        getPwdGrade: function (pwd) {
            var baseGrade = 20;
            var s_level = f.checkStrong(pwd);
            switch (s_level) {
                case 0:
                case 1:
                    baseGrade = 20;
                    break;
                case 2:
                    baseGrade = 40;
                    break;
                default:
                    baseGrade = 60;
                    break;
            }
            return baseGrade + "";
        }
    }
    return f;
})();

