flash.moe/public/key.php

26 lines
759 B
PHP
Raw Normal View History

2020-08-20 00:02:37 +00:00
<?php
2021-07-07 01:13:50 +00:00
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;
}
2020-08-20 00:02:37 +00:00
header('Content-Type: text/plain');
2021-07-07 01:13:50 +00:00
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;