PHP 随机密码生成器Shell脚本 - 基于PHP类+ SHA1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 随机密码生成器Shell脚本 - 基于PHP类+ SHA1相关的知识,希望对你有一定的参考价值。
#!/usr/bin/php
<?php
/**
* Generate a random password
*/
class Password
{
/**
* Generate the new password
*
* @access public
* @param array $params
* @return string
**/
public function generate($params = array())
{
$length = (!array_key_exists('length', $params)) ? 15 : $params['length'];
$use_lower = (!array_key_exists('use_lower', $params)) ? TRUE : $params['use_lower'];
$use_upper = (!array_key_exists('use_upper', $params)) ? TRUE : $params['use_upper'];
$use_number = (!array_key_exists('use_number', $params)) ? TRUE : $params['use_number'];
$use_custom = (!array_key_exists('use_custom', $params)) ? '!#%-_()' : $params['use_custom'];
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lower = "abcdefghijklmnopqrstuvwxyz";
$number = "0123456789";
$seed_length = 0;
$seed = '';
$password = '';
if($use_upper === TRUE){
$seed_length += 26;
$seed .= $upper;
}
if($use_lower === TRUE){
$seed_length += 26;
$seed .= $lower;
}
if($use_number === TRUE){
$seed_length += 10;
$seed .= $number;
}
if(!empty($use_custom)){
$seed_length +=strlen($use_custom);
$seed .= $use_custom;
}
for($i = 1; $i <= $length; $i++){
$password .= $seed{rand(0,$seed_length-1)};
}
return $password;
} // End of generate
} // End of Class Password
// ------------------------------------------------------------------------
/**
* With Special Characters
**/
echo "\nWith Special Characters: \n";
echo "-------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5)) . "\n";
for ($i=10; $i <= 80; $i += 10) {
$password = new Password;
echo $i . ': ' . $password->generate(array('length' => $i)) . "\n";
}
// ------------------------------------------------------------------------
/**
* Without Special Characters
**/
echo "\nWithout Special Characters: \n";
echo "----------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . "\n";
for ($i=10; $i <= 80; $i += 10) {
$password = new Password;
echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . "\n";
}
// ------------------------------------------------------------------------
/**
* SHA1 Based
* These passwords will only contain lowercase letters from a to f and numbers from 0 to 9
**/
$seed = sha1(uniqid(mt_rand(), true));
$hash1 = sha1(uniqid($seed . mt_rand(), true));
$hash2 = sha1(uniqid($seed . mt_rand(), true)).sha1(uniqid($hash1 . mt_rand(), true));
echo "\nSHA1 Based: \n";
echo "------------\n";
echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . "\n";
echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . "\n";
echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . "\n";
echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . "\n";
echo "40: ${hash1}\n";
echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . "\n";
echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . "\n";
echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . "\n";
echo "80: ${hash2}\n";
以上是关于PHP 随机密码生成器Shell脚本 - 基于PHP类+ SHA1的主要内容,如果未能解决你的问题,请参考以下文章