<?php
function generatePassword(int $length = 20, array $sets = ['lower', 'upper', 'digits', 'symbols']): string {
$alphabets = [
'lower' => 'abcdefghijklmnopqrstuvwxyz',
'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'digits' => '0123456789',
'symbols' => '!@#$%^&*()-_=+[]{};:,.<>?/~',
];
$alpha = '';
foreach ($sets as $s) $alpha .= $alphabets[$s] ?? '';
if ($alpha === '' || $length < 1) throw new InvalidArgumentException('Bad input');
$alphaLen = strlen($alpha);
$limit = intdiv(256, $alphaLen) * $alphaLen;
$out = '';
while (strlen($out) < $length) {
$b = ord(random_bytes(1));
if ($b < $limit) $out .= $alpha[$b % $alphaLen];
}
return $out;
}
echo generatePassword(16); // e.g. "X9k!nQ2pR&7vL@bW"
echo generatePassword(24, ['lower','upper','digits']); // no symbols
Create a free account and build your private vault. Share publicly whenever you want.