Link to home
Start Free TrialLog in
Avatar of Evert Jor
Evert JorFlag for Norway

asked on

Setting criteria for user passwords

I have the following lines of code that forces a minimum length of the password, but I need to enforce a stricter password policy where there has to be both letters and numbers in each password. I need to rewrite this snippet:

else if (strlen($_POST['password']) < 5) {

	$this->UserInterface->Error(true, 'Please choose password greater than 5');

	$this->Load->Validate($_userVerifyHashID);

	return false;

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

There's some code on this page that looks good:  http://paulcracknell.net/996/using-php-for-password-validation/   It checks both size and characters.
CPU's are fast so calling preg_match a million times has negligible impact however I am an efficiency nut - here is the code I use - it is quite versatile in terms of changing to meet requirements
<?php
define('MIN_LENGTH', 6);
define('MAX_LENGTH', 20);

$password = "PASs!2RDDD";

if (validatePassword($password) == 0x0f) {
  echo "Password is fine<br/>";
}
else {
  echo "Password is invalid<br/>";
}

function validatePassword($password, $min_length = MIN_LENGTH, $max_length=MAX_LENGTH) 
{
  $len = strlen($password);
  
  $check = 0;
  if ($len >= $min_length && $len <= $max_length) {
    // LOOP THROUGH THE PASSWORD CHECKING FOR VARIOUS
    // REQUIRED CHARS - IF FOUND WE SET A BIT FLAG
    // WE STOP LOOPING AFTER THE END OF STRING OR
    // ALL CONDITIONS ARE MET
    for($i=0; ($i < $len) && ($check != 0x0F); $i++) {
      if ($password[$i] >= 'a' && $password[$i] <= 'z') $check = $check | 0x01; // Lower Case
      if ($password[$i] >= 'A' && $password[$i] <= 'Z') $check = $check | 0x02; // Upper Case
      if ($password[$i] >= '0' && $password[$i] <= '9') $check = $check | 0x04; // Numeric
      if (($password[$i] >= '!' && $password[$i] <= '/') ||
            ($password[$i] >= ':' && $password[$i] <= '@') ||
            ($password[$i] >= '[' && $password[$i] <= '`') ||
            ($password[$i] >= '{' && $password[$i] <= '~')) $check = $check | 0x08; // Alt chars
    }
  }
  return $check; // Returns 0 if string is not the right length
}

Open in new window

The nice thing about this code is that the return value tells you what is missing from the password.

If bit 0 is not set you are missing a lower case char
If bit 1 is not set you are missing an upper case char
etc
To find out what you are missing simply XOR with 0x0F which gives you a number that can be translated back into a condition.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All that password variant stuff is utter nonsense.  The plain fact is that you can make life "password hell" for your clients by making up capricious rules, but you can't outrun the cryptologists (good or bad) that seek to uncover and crack your passwords.  A better strategy might be a "passphrase" with a minimum word length.  If you want to see how unsuccessful you are likely to be in controlling and concealing passwords, have a look at this article.  It's rather old news by now.
http://arstechnica.com/security/2013/05/how-crackers-make-minced-meat-out-of-your-passwords/

I wish I had a good solution to offer, but with current technology and the minimalist password rules like "one upper, one lower, one punctuation, one number" you're out on thin ice, and if what you're trying to protect has any value you need to think about much more than just a password.