Click to See Complete Forum and Search --> : Product "serial number" generation...


big.nerd
05-11-2007, 12:03 AM
I was just trying something out.. for the record, no im not planning on having people pay for any of MY scripts, that would be a crime.

But just for fun I wondered how one could make a script that takes the name, and generates a KEY, or serial number.

Here is what I have so far (yes, its horrible).


<?php
$name = "Big Nerd";
echo $name."<br>";
$key = "";
$sl = strlen($regname);
$key = $sl;
$sp = ($sl / 2);
$rn = substr($regname,$sp,1);
$rx = md5($rn);
$rp = strtoupper(substr($rx,1,2));
$key.= $rp."-";
if (strlen($key) <= 4) {
$key ="Z".$key;
}
$ax = ($sl + 12);
$key.=$ax;
$key.=strtoupper(substr($rx,$sl,2));
$key.="-".round($sl*10/2+8/9*102/12,0);
$key.=rand(66,71)."-";
$rkey = md5($key);
$key.= strtoupper(substr($rkey,6,4));
$xkey.= crypt($key,$sl);
$xkey = strtoupper($xkey);
$xkey = substr($xkey,2,4);
$key.="-".$xkey;
echo $key;
?>


Now, just for fun, I used a obfuscator to see the plausability of this style of generating a key (which it makes one that looks pretty, anyways).

I could break it down & make a "keygen" for it myself.

I find it uses too many functions, it is definately not feasable for use as it is far from optimized...

Any ideas on how to make this better?

WesRatcliff
05-11-2007, 06:38 AM
Here's how I did it...

function _generate_api_key()
{
require_once 'Text/Password.php';
do {
$api_key = strtoupper(implode('-', Text_Password::createMultiple(5, 5, 'unpronounceable', 'alphanumeric')));
} while ($this->retrieve_by_api_key($api_key));
return $api_key;
}

The docs for PEAR::Text_Password are here (http://pear.php.net/manual/en/package.text.text-password.php).

By using Text_Password you are able to fine-tune what characters end up in your serial number. For example, you can exclude "0", "o" and "O" so that the customer does not get confused.

Wes

big.nerd
05-11-2007, 01:56 PM
WesRatcliff,

Thanks, I will download that just to see how they did it. It was more for informative / string manipulation purposes.