25 lines
759 B
PHP
25 lines
759 B
PHP
<?php
|
|
define('KEY_CHARS', 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789');
|
|
define('KEY_CHARS_LENGTH', strlen(KEY_CHARS));
|
|
|
|
function generateKey(int $length): string {
|
|
$bytes = random_bytes($length);
|
|
for($i = 0; $i < $length; ++$i)
|
|
$bytes[$i] = KEY_CHARS[ord($bytes[$i]) % KEY_CHARS_LENGTH];
|
|
return $bytes;
|
|
}
|
|
|
|
header('Content-Type: text/plain');
|
|
|
|
if(isset($_GET['length'])) {
|
|
$length = (int)filter_input(INPUT_GET, 'length', FILTER_SANITIZE_NUMBER_INT);
|
|
if($length > 0 && $length < 1000) {
|
|
echo generateKey($length);
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo '16: ' . generateKey(8) . PHP_EOL;
|
|
echo '32: ' . generateKey(16) . PHP_EOL;
|
|
echo '64: ' . generateKey(32) . PHP_EOL;
|
|
echo '128: ' . generateKey(64) . PHP_EOL;
|