Solved

Setting criteria for user passwords

Posted on 2014-10-16
4
88 Views
Last Modified: 2014-11-27
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

0
Comment
Question by:EvertJor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40386087
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.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 40386132
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.
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40386142
Hmm - just ran some performance tests against posting above and one using preg_match - preg_match won hands down.
So alternative using preg_match
function validatePassword2($password, $min_length = MIN_LENGTH, $max_length=MAX_LENGTH) 
{
  $len = strlen($password);
  if ($len >= $min_length && $len <= $max_length) {
    if( !preg_match("#[0-9]+#", $password) ) return -1;
    if( !preg_match("#[a-z]+#", $password) ) return -2;
    if( !preg_match("#[A-Z]+#", $password) ) return -3;
    if( !preg_match("#\p{P}|\p{S}+#", $password) ) return -4;
  }
  else {
  return -99; // String too long or too short
  }
  return 0;
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40386714
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.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question