Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Input: password
- // Output:
- // on success: true
- // on failure: error message
- function passwordIsValid(password)
- {
- var mostCommonPasswords = ["password", "baseball", "dragon", "football", "monkey",
- "letmein", "abc123", "mustang", "access", "shadow", "master", "michael",
- "superman", "696969", "123123", "batman", "trustno1"];
- var passwordTooEasy = function(pass) {
- // --- HELPER FUNCTION --- //
- var allElementsAreEqual = function(array) { // array.length >= 1
- var firstEl = array[0];
- var allEqual = true;
- for (var i = 1; i < array.length; i++)
- if (array[i] !== firstEl)
- return false;
- return true;
- };
- // --- CHECK 1 --- //
- // check if composed of a simple sequence of 123456/qwerty combination
- // if password length is up to 12, there are only ~80 passwords which will fail
- // q123w4e56 or similar is still considered OK (details in comments for sequence)
- // if password length is up to 12, there are 2048 - ~80 {2 ^ 12 - ~80} passwords like this
- var firstSequence = "1234567890";
- var firstSequencePosition = 0;
- var secondSequence = "qwertyuiop";
- var secondSequencePosition = 0;
- // each element will be the number of consecutive characters from any sequence
- // for example, pass=123qwe456rty translates to [3, 3, 3, 3]
- // validation fails if all numbers in sequence are the same
- var sequence = [];
- var lastCharacter = -1;
- var allCharactersFromSequence = true;
- for (var i = 0; i < pass.length; i++)
- {
- var character = pass[i];
- var found = -1;
- if (character === firstSequence[firstSequencePosition])
- {
- found = 0;
- firstSequencePosition++;
- }
- else if (character === secondSequence[secondSequencePosition])
- {
- found = 1;
- secondSequencePosition++;
- }
- else // validation passed
- {
- allCharactersFromSequence = false;
- }
- // record the found character
- if (found === lastCharacter) {
- sequence[sequence.length - 1]++;
- } else {
- sequence.push(1);
- lastCharacter = found;
- }
- }
- if (allCharactersFromSequence && allElementsAreEqual(sequence))
- return true;
- // --- CHECK 2 --- //
- // check if all letters are equal
- if (allElementsAreEqual(pass))
- return true;
- // all checks passed, password is (hopefully) not too easy
- return false;
- };
- if (password.length <= 3) return "Password too short (need 4 or more characters)";
- else if ($.inArray(password, mostCommonPasswords) != -1) return "Password too common";
- else if (passwordTooEasy(password)) return "Password too easy";
- else return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement