Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // Input: password
  2. // Output:
  3. // on success: true
  4. // on failure: error message
  5. function passwordIsValid(password)
  6. {
  7. var mostCommonPasswords = ["password", "baseball", "dragon", "football", "monkey",
  8. "letmein", "abc123", "mustang", "access", "shadow", "master", "michael",
  9. "superman", "696969", "123123", "batman", "trustno1"];
  10.  
  11. var passwordTooEasy = function(pass) {
  12. // --- HELPER FUNCTION --- //
  13. var allElementsAreEqual = function(array) { // array.length >= 1
  14. var firstEl = array[0];
  15. var allEqual = true;
  16.  
  17. for (var i = 1; i < array.length; i++)
  18. if (array[i] !== firstEl)
  19. return false;
  20.  
  21. return true;
  22. };
  23.  
  24. // --- CHECK 1 --- //
  25. // check if composed of a simple sequence of 123456/qwerty combination
  26. // if password length is up to 12, there are only ~80 passwords which will fail
  27. // q123w4e56 or similar is still considered OK (details in comments for sequence)
  28. // if password length is up to 12, there are 2048 - ~80 {2 ^ 12 - ~80} passwords like this
  29.  
  30. var firstSequence = "1234567890";
  31. var firstSequencePosition = 0;
  32. var secondSequence = "qwertyuiop";
  33. var secondSequencePosition = 0;
  34.  
  35. // each element will be the number of consecutive characters from any sequence
  36. // for example, pass=123qwe456rty translates to [3, 3, 3, 3]
  37. // validation fails if all numbers in sequence are the same
  38. var sequence = [];
  39. var lastCharacter = -1;
  40.  
  41. var allCharactersFromSequence = true;
  42.  
  43. for (var i = 0; i < pass.length; i++)
  44. {
  45. var character = pass[i];
  46. var found = -1;
  47. if (character === firstSequence[firstSequencePosition])
  48. {
  49. found = 0;
  50. firstSequencePosition++;
  51. }
  52. else if (character === secondSequence[secondSequencePosition])
  53. {
  54. found = 1;
  55. secondSequencePosition++;
  56. }
  57. else // validation passed
  58. {
  59. allCharactersFromSequence = false;
  60. }
  61. // record the found character
  62. if (found === lastCharacter) {
  63. sequence[sequence.length - 1]++;
  64. } else {
  65. sequence.push(1);
  66. lastCharacter = found;
  67. }
  68. }
  69. if (allCharactersFromSequence && allElementsAreEqual(sequence))
  70. return true;
  71.  
  72. // --- CHECK 2 --- //
  73. // check if all letters are equal
  74. if (allElementsAreEqual(pass))
  75. return true;
  76.  
  77. // all checks passed, password is (hopefully) not too easy
  78. return false;
  79. };
  80.  
  81. if (password.length <= 3) return "Password too short (need 4 or more characters)";
  82. else if ($.inArray(password, mostCommonPasswords) != -1) return "Password too common";
  83. else if (passwordTooEasy(password)) return "Password too easy";
  84. else return true;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement