www.webdeveloper.com
Results 1 to 4 of 4

Thread: Which is best for generating serial keys?

  1. #1
    Join Date
    Aug 2014
    Posts
    2

    Question 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, "-"), "-");
    }
    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, "-"));
    }
    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.

    :-)

  2. #2
    Join Date
    Aug 2014
    Posts
    2
    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?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,636
    I just felt like playing around with a different variation, to use/ignore at your discretion:
    PHP Code:
    <?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), 1036);
        }
        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;
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2014
    Posts
    77
    I would remove any i I l L o O 1 0, these tend to give people too many headaches trying to figure out which is which.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles