Solved

check passwords

Posted on 2014-11-10
3
60 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
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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.

947 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

23 Experts available now in Live!

Get 1:1 Help Now