Solved

Setting criteria for user passwords

Posted on 2014-10-16
4
82 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
  • 2
4 Comments
 
LVL 82

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 51

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 51

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now