0

When I generate an array of characters from a "valid character array", the final result is a random set of characters but IN ORDER. For example, I will get 34579abopxzBCJMSTXY.

include('db.php');
//$num= mt_rand();
$veces = $_POST['veces'];
$equipos = $_POST['equipos'];
$description = $_POST['description'];
$salt = "oijahsfdapsf80efdjnsdjp";
function get_result(){
$valid_chars=array("0","1", "2", "3", "4", "5", "6", "7", "8", "9"
, "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t",     "u", "v", "w", "x", "y", "z"
, "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

$result=array_rand($valid_chars, 20); //Cantidad de Caracteres
$result_str='';
foreach ($result as $value)
{
$result_str.=$valid_chars[$value];
}
return $result_str;
}
$results=array();

for ($i=0; $i<=2000; $i++)
{
array_push($results, get_result());
}

$results=array_unique($results);

$cont=0;
$results_str='';
foreach ($results as $value)
{
$cont++;
$results_str.=$value.'
';
// Add some salt
$salt = "oijahsfdapsf80efdjnsdjp";
$salt .= $value; //oijahsfdapsf80efdjnsdjp_Plus_RandomString
$value = $salt;  //Change the RandomString to contain our new salted pass
$value = md5($value);  //( cannot be reversed on a rainbow table as we have a secret salt also.. )
$db = new Database();
$ins_query = "INSERT INTO licensing(license, computers, state, description) VALUES('{$value}', '{$equipos}', '2', '{$description}')";    // query to insert value
$db->query($ins_query);
if ($cont>=$veces)
    {
    break;
    }
}
echo '<pre>';
echo $results_str;
echo '</pre>';

PHP Information Version 5.3.3

Output Example:

1389adehtvwCEKMNQSUY
15bgmnpruwzAEFHTUVWY
159bcenqrswxzFKMNQUX
35bchstvwxyzEGKNQSUV
124789defgmvyDEHKNRT
5abefhjprsuwxCDMPQXY
1
  • 1
    It's time to stop using MD5 for cryptographic purposes. There are MUCH better alternatives out there. Use one of them instead. Commented Feb 26, 2014 at 19:05

2 Answers 2

2

you can use the shuffle(array) on your valid_char variable to randomize all the elements http://fr.php.net/manual/en/function.shuffle.php

Sign up to request clarification or add additional context in comments.

Comments

1
$valid_chars=array("0","1", "2", "3", "4", "5", "6", "7", "8", "9"
, "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t",     "u", "v", "w", "x", "y", "z"
, "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

$result=array_rand($valid_chars, 20);
$i=0;
foreach ($result as $value)
{
$result[$i]=$valid_chars[$value];
$i++;
}


foreach ($result as $v) {
    echo "$v";
}
 echo "<br>";

shuffle($result);
foreach ($result as $v) {
    echo "$v";
}

this will echo

0128acfknqrsxCJMPQRS    //your string in "order"
PrMxR0k8nqJCQcS2sfa1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.