Solved

check passwords

Posted on 2014-11-10
3
66 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
[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
3 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40433479
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 110

Expert Comment

by:Ray Paseur
ID: 40433815
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40434001
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

628 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