Solved

Setting criteria for user passwords

Posted on 2014-10-16
4
84 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 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 54

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 54

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 109

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 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