Solved

check passwords

Posted on 2014-11-10
3
58 Views
Last Modified: 2014-11-18
Im a bit stuck how to sort a problem Ive been given. Ive been asked to write a function to check a password, Ive written everything else but stuck on the rule
No 4 conservative characters from your previous 2 passwords

So something like a new password "banannas" that checks with "bana332" will fail cause "bana" exists.

Ive got the last 2 entries of passwords stored in an array, but stuck how to compare the passwords with every option.

Anyone got any pointers or sample code?????
0
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
You can convert each of the strings to ann array with

$pass1=str_split($str1);
pass2=str_split($str2);

Loop through the array for the new pass word and compare each character to the contents of the old password with: in_array();

Cd&
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
No 4 conservative characters from your previous 2 passwords
Do you mean consecutive characters?  Do you have the previous 2 passwords at hand (perhaps stored in a data base table)?  If you do it's quite likely that an exposure of your data base would reveal all of the client passwords, which is why we do not store passwords in clear text.

Please tell us a little more about the assignment.  It sounds like there is something fishy going on.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
I don't have time to monitor this one tonight, so here is my take on a possible code example and test case.  Best of luck with it, ~Ray
http://iconoun.com/demo/temp_tonelm54.php

<?php // demo/temp_tonelm54.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28554387.html
// REF: http://php.net/manual/en/function.in-array.php
// REF: http://php.net/manual/en/function.substr.php

function in_substring($needle, $haystack, $length=4, $case=TRUE)
{
    // IF CASE-INSENSITIVE, NORMALIZE THE DATA PRIOR TO COMPARISON
    if (!$case)
    {
        $needle   = strtoupper($needle);
        $haystack = strtoupper($haystack);
    }

    // MAKE THE SUBSTRINGS OF THE NEEDLE
    while ($needle_parts[] = substr($needle, 0, $length))
    {
        $needle = substr($needle, 1);
        if (strlen($needle) < $length) break;
    }

    // MAKE THE SUBSTRINGS OF THE HAYSTACK
    while ($haystack_parts[] = substr($haystack, 0, $length))
    {
        $haystack = substr($haystack, 1);
        if (strlen($haystack) < $length) break;
    }

    // LOOK FOR NEEDLE PARTS IN THE HAYSTACK PARTS
    foreach ($needle_parts as $needle_part)
    {
        if (in_array($needle_part, $haystack_parts))
        {
            return TRUE;
        }
    }
    return FALSE;
}

// TEST CASES
$tests = array
( // FIND abc IN ABCDE case-insensitive
  (object)array
  ( 'needle'=> 'abc'
  , 'haystack' => 'ABCDE'
  , 'len' => 3
  , 'case' => FALSE
  )
, // FIND abc IN ABCDE case-Sensitive
  (object)array
  ( 'needle'=> 'abc'
  , 'haystack' => 'ABCDE'
  , 'len' => 3
  , 'case' => TRUE
  )
, // FIND thump IN thing
  (object)array
  ( 'needle'=> 'thump'
  , 'haystack' => 'thing'
  , 'len' => 4
  , 'case' => TRUE
  )
, // FIND th IN thing
  (object)array
  ( 'needle'=> 'th'
  , 'haystack' => 'thing'
  , 'len' => 2
  , 'case' => TRUE
  )
, // FIND bana IN Bananas Case-insensitive
  (object)array
  ( 'needle'=> 'BANA'
  , 'haystack' => 'BaNaNas'
  , 'len' => 4
  , 'case' => FALSE
  )
, // FIND bana IN Bananas Case-Sensitive
  (object)array
  ( 'needle'=> 'bana'
  , 'haystack' => 'Bananas'
  , 'len' => 4
  , 'case' => TRUE
  )
)
;
foreach ($tests as $test)
{
    echo PHP_EOL . '<br>';
    echo $test->needle;
    echo ' IN ';
    echo $test->haystack;
    echo '? ';
    echo "(LEN=$test->len, CASE=$test->case) ";
    echo '<b>';
    var_dump(in_substring($test->needle, $test->haystack, $test->len, $test->case));
    echo '</b>';
    echo PHP_EOL;
}

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now