Skip to content

Instantly share code, notes, and snippets.

@fantasyczl
Last active October 15, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantasyczl/dd46cf5e38bde62a95bceb7accaf2692 to your computer and use it in GitHub Desktop.
Save fantasyczl/dd46cf5e38bde62a95bceb7accaf2692 to your computer and use it in GitHub Desktop.
生成随机密码
<?php
/**
* Generate Password
*/
function generate_password($len)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?";
$max = strlen($chars) - 1;
$pw = '';
for ($i = 0; $i < $len; $i++) {
$r = random_int(0, $max);
$pw .= $chars[$r];
}
return $pw;
}
if (!empty($argv[1])) {
$len = $argv[1];
} else {
$len = 20;
}
$pw = generate_password($len);
echo "password: $pw" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment