/    Sign up×
Community /Pin to ProfileBookmark

Which is best for generating serial keys?

Hi, so i’ve made a function called GenerateKey, and i’ve made 2 of them to see which solution was best, and i like the second one best.
But how big is the difference on speed?
Here’s the first version:

[CODE]function GenerateKey($name)
{
$generated = strtoupper(md5($name).uniqid());
return rtrim(chunk_split(substr($generated, -24), 4, “-“), “-“);
}[/CODE]

Then the second version(Which i like best):

[CODE]function GenerateKey($length)
{
$random = array(‘q’,’w’,’e’,’r’,’t’,’y’,’u’,’i’,’o’,’p’,’a’,’s’,’d’,’f’,’g’,’h’,’j’,’k’,’l’,’z’,’x’,’c’,’v’,’b’,’n’,’m’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′);
$serial = “”;

for ($i=0; $i < $length; $i++) {
$randomChar = $random[mt_rand(0, 35)];

if(strlen($serial) < 20)
{
$serial .= $randomChar;
}
}
$key = chunk_split($serial, 4, “-“);

return strtoupper(rtrim($key, “-“));
}[/CODE]

I know the $key variable is not needed, but i will change that later.

So how’s the speed, which is fastest, i bet the first one but i just like the second one best,
and the result of the second one is so much better.

🙂

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jolo309authorAug 23.2014 — Can't seem to find the edit button, not sure if allowed to edit yet because i just registered.

Anyway, i think the first solution is slower, i just ran a test, but i'm not sure if i'm calculating it correctly

http://imgur.com/53mh7eR

The bottom one is the first solution and the upper one is the second solution, so maybe the second solution is fastest?
Copy linkTweet thisAlerts:
@NogDogAug 24.2014 — I just felt like playing around with a different variation, to use/ignore at your discretion:
[code=php]
<?php

function getRandSerial($numChunks=4, $separator='-')
{
$min = 36*36*36;
$max = 36*36*36*36-1;
$chunks = array();
for($i=0; $i<$numChunks; $i++)
{
$chunks[] = base_convert(rand($min, $max), 10, 36);
}
return implode($separator, $chunks);
}
// test
for($i=1; $i<=10; $i++)
{
echo getRandSerial().PHP_EOL;
echo getRandSerial($i).PHP_EOL;
echo getRandSerial($i, '.').PHP_EOL;
}
[/code]
Copy linkTweet thisAlerts:
@GravyAug 24.2014 — I would remove any [B]i I l L o O 1 0[/B], these tend to give people too many headaches trying to figure out which is which.
×

Success!

Help @jolo309 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...