J
J
John Freeman2015-09-28 11:51:32
PHP
John Freeman, 2015-09-28 11:51:32

How to generate a key?

Hello!
The essence of the question is this:
How can I make a key generator so that I can use the secret key, and that the key would be XXXXX-XXXXX-XXXXX-XXXXX on exit?
used simple code before:

<?php
function sernum()
{
    $template   = 'XXX99-XXX99-99XXX-99XXX';
    $k = strlen($template);
    $sernum = '';
    for ($i=0; $i<$k; $i++)
    {
        switch($template[$i])
        {
            case 'X': $sernum .= chr(rand(65,90)); break;
            case '9': $sernum .= rand(0,9); break;
            case '-': $sernum .= '-';  break; 
        }
    }
    return $sernum;
}
echo '<pre>';

for ($i=0; $i < 10; $i++) echo sernum(), '<br/>';

echo '</pre>';
?>

and wrote the keys to the database. Further already worked with a DB.
but now you need to add a secret key (word)
to it so that you can generate it in PHP and C # and check the validation,
for example, I’ll just check it in PHP, check the record in the database, and through the application in C # you need to check offline!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-09-28
@AsviS

For example, you can do this: https://3v4l.org/LHe5q

<?php

define('SECRET', 'tostersecretcode2015');

/**
 * Создаёт серийный номер с заданной проверочной частью.
 * 
 * @param string $check Часть серийного номера, используемая для проверки.
 * 
 * @return bool
 */
function sernum ($check = null)
{
    $template   = 'XXX99-XXX99-99XXX-99XXX';
    
    $parts = explode('-', $template, 2);
    if (!isset($check)) {
        $check = '';
        for ($i = 0; $i < strlen($parts[0]); $i++) {
            switch ($parts[0][$i]) {
                case 'X': $check .= chr(rand(65, 90)); break;
                case '9': $check .= strval(rand(0,9)); break;
            }
        }
    }
    
    $sernum = $check . '-';
    $hash = hash('sha256', $check . SECRET);
    
    for ($i = 0; $i < strlen($parts[1]); $i++) {
        switch ($parts[1][$i]) {
            case 'X': $sernum .= chr(65 + ord($hash[$i]) % 26); break;
            case '9': $sernum .= strval(ord($hash[$i]) % 10); break;
            case '-': $sernum .= '-';  break; 
        }
    }
    
    return $sernum;
}

/**
 * Проверяет серийный номер.
 * 
 * @return bool
 */
function check_sernum ($sernum)
{
    $parts = explode('-', $sernum, 2);
    return (sernum($parts[0]) === $sernum);
}


echo '<pre>Десять случайных номеров:<br>';
for ($i = 0; $i < 10; $i++) 
    echo sernum(), '<br/>';

if (check_sernum('XCC58-AYA68-75ZUU-19TDZ')) {
    echo 'Номер XCC58-AYA68-75ZUU-19TDZ прошёл проверку<br>';
}

if (!check_sernum('ESJ18-TBZ25-42XDX-38XWY')) {
    echo 'Номер ESJ18-TBZ25-42XDX-38XWY не прошёл проверку<br>';
}

Algorithm for creating a serial number:
1. Divide the template XXX99-XXX99-99XXX-99XXX into two parts: XXX99 and XXX99-99XXX-99XXX
2. For the first part of XXX99, use random letters and numbers. We get, for example: XCC58
3. Calculate the SHA256 hash for the string ('XCC58' . SECRET)
4. Use the character codes of the received hash to get the rest of the key, and adjust it to the pattern XXX99-99XXX-99XXX. We get AYA68-75ZUU-19TDZ.
5. We combine the two received lines - the serial number is ready.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question