Answer the question
In order to leave comments, you need to log in
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>';
?>
Answer the question
In order to leave comments, you need to log in
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>';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question