Page 1 of 1

Obfuscation challenge

Posted: Fri Nov 02, 2007 12:33 am
by alex.barylski
I've given a quick attempt:

Code: Select all

function generateUniqueId()
{
  static $entry = 0;
  static $index = 0;

  $valid = array('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789');

  if($entry == 0){
    $value = $valid{0}; // First time funciton is called make sure we use lower case 'a' as the first character
    $entry = 1;
    $index = 1; // Use next character in array next call
  }
  else{

    // NOTE: I'm starting to become discombobulated - so I'll leave it here 
    if($index < strlen($valid) && $depth){
      $value = $valid{$index};
    }
      
    $index++;
  }
  
  return $value;
}
So the source is incomplete...but it demonstrates the idea...

Basically the idea is to generate a unique variable but instead of generating lengthly ID's such as 7EH3DHBFYRF7RBFVGHY I want to generate names in the following pattern:

a
b
c
d
e
..
_

Then...

aa
ab
ac
ad
ae
af
ag
ah
ai

Then...

ba
bb
bc
bd
be
bf
bg
...

Then...

ca
cb
cc
cd
ce
...

You get the idea.

I'm curious to see what others might come up with...

p.s-I use statics because the unique ID is generated on each call to the function.

Anybody?

Posted: Fri Nov 02, 2007 1:16 pm
by Kieran Huggins
a faster (albeit less compact) solution would be to use:

Code: Select all

function generateUniqueId(){
  static $var = 'a';
  return $var++;
}

Posted: Fri Nov 02, 2007 2:49 pm
by alex.barylski
Haha...

Thats what I used initially. The variable names grow to tremdous size though, so I scrapped the idea. :P

p.s-I didn't think this message made it into DevNet. With all the errors I've been getting lately about half my messages don't take the first time round so I just forget it and pretend it never happened.

Posted: Fri Nov 02, 2007 2:55 pm
by Kieran Huggins
the code I posted is still pretty compact, it produces:

Code: Select all

a
b
c
.
.
.
x
y
z
aa
ab
ac
.
.
.
ay
az
ba
bb
.
.
.
The only difference is the lack of capitals and digits.