密码强度计

我有一种情况,我希望能够在我的系统的networking界面中对用户密码进行评分,以便在他们提交之前知道他们是否有错误的密码。

关键要求:

  • 必须能够评分密码,而不仅仅是通过/失败。
  • 如果密码在阈值以下,应该禁用表单,所以用户不能提交。
  • 看起来不错。 🙂
  • 不使用jQuery – 我们目前在这个系统中使用Mochikit和Y!UI。

我发现许多使用jQuery编写的密码表,以及诸如http://www.passwordmeter.com/的内容太冗长。

任何人都可以build议一个很好的下降,我可以使用javascript密码评估者,或者给一个如何写一个例子?

这是一个脚本的集合: http : //webtecker.com/2008/03/26/collection-of-password-strength-scripts/

我认为他们都评价密码,不使用jQuery …但我不知道他们是否有本机支持禁用表单?

更新 :创build一个js小提琴在这里看到它住: http : //jsfiddle.net/HFMvX/

我经历了谷歌search吨,没有发现任何令人满意的。 我喜欢passpack如何做到这一点,所以基本上是反向devise他们的方法,在这里我们去:

function scorePassword(pass) { var score = 0; if (!pass) return score; // award every unique letter until 5 repetitions var letters = new Object(); for (var i=0; i<pass.length; i++) { letters[pass[i]] = (letters[pass[i]] || 0) + 1; score += 5.0 / letters[pass[i]]; } // bonus points for mixing it up var variations = { digits: /\d/.test(pass), lower: /[az]/.test(pass), upper: /[AZ]/.test(pass), nonWords: /\W/.test(pass), } variationCount = 0; for (var check in variations) { variationCount += (variations[check] == true) ? 1 : 0; } score += (variationCount - 1) * 10; return parseInt(score); } 

良好的密码开始得分大约60左右,这里的function来翻译的话:

 function checkPassStrength(pass) { var score = scorePassword(pass); if (score > 80) return "strong"; if (score > 60) return "good"; if (score >= 30) return "weak"; return ""; } 

你可能想调整这一点,但我发现它适合我很好

 Password Strength Algorithm: Password Length: 5 Points: Less than 4 characters 10 Points: 5 to 7 characters 25 Points: 8 or more Letters: 0 Points: No letters 10 Points: Letters are all lower case 20 Points: Letters are upper case and lower case Numbers: 0 Points: No numbers 10 Points: 1 number 20 Points: 3 or more numbers Characters: 0 Points: No characters 10 Points: 1 character 25 Points: More than 1 character Bonus: 2 Points: Letters and numbers 3 Points: Letters, numbers, and characters 5 Points: Mixed case letters, numbers, and characters Password Text Range: >= 90: Very Secure >= 80: Secure >= 70: Very Strong >= 60: Strong >= 50: Average >= 25: Weak >= 0: Very Weak 

设置如果要更改密码中选中的内容,请切换为true或false

 var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz"; var m_strNumber = "0123456789"; var m_strCharacters = "!@#$%^&*?_~" Check password function checkPassword(strPassword) { // Reset combination count var nScore = 0; // Password length // -- Less than 4 characters if (strPassword.length < 5) { nScore += 5; } // -- 5 to 7 characters else if (strPassword.length > 4 && strPassword.length < 8) { nScore += 10; } // -- 8 or more else if (strPassword.length > 7) { nScore += 25; } // Letters var nUpperCount = countContain(strPassword, m_strUpperCase); var nLowerCount = countContain(strPassword, m_strLowerCase); var nLowerUpperCount = nUpperCount + nLowerCount; // -- Letters are all lower case if (nUpperCount == 0 && nLowerCount != 0) { nScore += 10; } // -- Letters are upper case and lower case else if (nUpperCount != 0 && nLowerCount != 0) { nScore += 20; } // Numbers var nNumberCount = countContain(strPassword, m_strNumber); // -- 1 number if (nNumberCount == 1) { nScore += 10; } // -- 3 or more numbers if (nNumberCount >= 3) { nScore += 20; } // Characters var nCharacterCount = countContain(strPassword, m_strCharacters); // -- 1 character if (nCharacterCount == 1) { nScore += 10; } // -- More than 1 character if (nCharacterCount > 1) { nScore += 25; } // Bonus // -- Letters and numbers if (nNumberCount != 0 && nLowerUpperCount != 0) { nScore += 2; } // -- Letters, numbers, and characters if (nNumberCount != 0 && nLowerUpperCount != 0 && nCharacterCount != 0) { nScore += 3; } // -- Mixed case letters, numbers, and characters if (nNumberCount != 0 && nUpperCount != 0 && nLowerCount != 0 && nCharacterCount != 0) { nScore += 5; } return nScore; } // Runs password through check and then updates GUI function runPassword(strPassword, strFieldID) { // Check password var nScore = checkPassword(strPassword); // Get controls var ctlBar = document.getElementById(strFieldID + "_bar"); var ctlText = document.getElementById(strFieldID + "_text"); if (!ctlBar || !ctlText) return; // Set new width ctlBar.style.width = (nScore*1.25>100)?100:nScore*1.25 + "%"; // Color and text // -- Very Secure /*if (nScore >= 90) { var strText = "Very Secure"; var strColor = "#0ca908"; } // -- Secure else if (nScore >= 80) { var strText = "Secure"; vstrColor = "#7ff67c"; } // -- Very Strong else */ if (nScore >= 80) { var strText = "Very Strong"; var strColor = "#008000"; } // -- Strong else if (nScore >= 60) { var strText = "Strong"; var strColor = "#006000"; } // -- Average else if (nScore >= 40) { var strText = "Average"; var strColor = "#e3cb00"; } // -- Weak else if (nScore >= 20) { var strText = "Weak"; var strColor = "#Fe3d1a"; } // -- Very Weak else { var strText = "Very Weak"; var strColor = "#e71a1a"; } if(strPassword.length == 0) { ctlBar.style.backgroundColor = ""; ctlText.innerHTML = ""; } else { ctlBar.style.backgroundColor = strColor; ctlText.innerHTML = strText; } } // Checks a string for a list of characters function countContain(strPassword, strCheck) { // Declare variables var nCount = 0; for (i = 0; i < strPassword.length; i++) { if (strCheck.indexOf(strPassword.charAt(i)) > -1) { nCount++; } } return nCount; } 

您可以根据自己的要求自行定制。