I want to be able to input a number and get a password, built from a string or unique characters. So if I have two characters in the string : $string = "AB"; these are the desired results :
-in-|-out-
0 | A
1 | B
2 | AA
3 | AB
4 | BA
5 | BB
6 | AAA
7 | AAB
8 | ABA
9 | ABB
10 | BBB
And so on. Here is my current code :
for($i = 1; $i < 100; $i++)
{
echo createString ($i, "AB")."<br/>";
}
function createString ($id, $chars) // THE ISSUE <---
{
$length = getLength($id, $chars);
//echo "LENGTH : ".$length."<br/><br/>";
$string = "";
for($i = 0; $i < $length; $i++)
{
$a = round(($id - 1)/pow($length, $i)); // THE ISSUE <-----
$local = local($a, strlen($chars));
$string = $chars{$local - 1}." : ".$string;
}
return $string;
}
function local ($num, $max)
{
$num += $max;
while($num > $max)
{
$num -= $max;
}
return $num;
}
/*
get the length of the output by inputing the "in" and defining the possible characters
*/
function getLength ($id, $chars)
{
$charNUM = 1;
$LR = -1;
$HR = 0;
while(true)
{
$LR = $HR;
$HR = pow(strlen($chars), $charNUM) + $LR;
$LR += 1;
//echo $LR." : ".$HR." : ".$charNUM."<br/>";
if($id >= $LR && $id <= $HR)
{
return $charNUM;
}
if($id < $LR)
{
return false;
}
$charNUM ++;
}
}
That outputs :
B :
A :
A : B :
B : A :
B : B :
A : A :
A : B : B :
A : B : A :
A : A : B :
A : A : A :
A : A : B :
A : B : A :
A : B : B :
A : B : A :
B : A : B : B :
B : A : B : A :
B : A : B : B :
B : A : B : A :
B : A : A : B :
B : A : A : A :
B : A : A : B :
B : A : A : A :
B : A : B : B :
B : A : B : A :
B : B : B : B :
B : B : B : A :
B : B : A : B :
B : B : A : A :
B : B : A : B :
B : B : A : A :
B : B : A : B : B :
B : B : A : B : A :
B : B : A : B : B :
B : B : A : A : A :
B : B : A : A : B :
B : B : A : A : A :
B : B : A : A : B :
B : B : A : A : A :
B : B : B : B : B :
B : B : B : B : A :
B : B : B : B : B :
And so on. But it has repeats. I am having issues with the function createString(). I want to access a password somewhere down a brute force password table without having it pre-computed. I don't want a pre-computed array and just access a point of it.
I will post here the code for translating any positive integer into a system of any given positive integer base (>1) returning the values of each digit.
So you would call use this function as follows:
Try it out. It is formatted a little different from your output but should be readable though.
Some expamle output for the base "AB":
Good old recursion
Solution:
Example:
Output:
Example 2:
Output 2:
Credit for
id_encode()
goes to @PatrickLorio.id_decode()
was built by myself with calculating by hand first:After that I think its possible to optimize
id_encode()
by taking the n-th root of the id... maybe I find the time to check that.