?
Solved

Setting criteria for user passwords

Posted on 2014-10-16
4
Medium Priority
?
90 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:Evert Jor
[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 84

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 58

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 58

Accepted Solution

by:
Julian Hansen earned 1500 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 111

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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it 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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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