随机密码生成器Shell脚本-基于PHP类+SHA1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机密码生成器Shell脚本-基于PHP类+SHA1相关的知识,希望对你有一定的参考价值。

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. /**
  5. * Generate a random password
  6. */
  7. class Password
  8. {
  9. /**
  10. * Generate the new password
  11. *
  12. * @access public
  13. * @param array $params
  14. * @return string
  15. **/
  16. public function generate($params = array())
  17. {
  18.  
  19. $length = (!array_key_exists('length', $params)) ? 15 : $params['length'];
  20. $use_lower = (!array_key_exists('use_lower', $params)) ? TRUE : $params['use_lower'];
  21. $use_upper = (!array_key_exists('use_upper', $params)) ? TRUE : $params['use_upper'];
  22. $use_number = (!array_key_exists('use_number', $params)) ? TRUE : $params['use_number'];
  23. $use_custom = (!array_key_exists('use_custom', $params)) ? '!#%-_()' : $params['use_custom'];
  24.  
  25. $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  26. $lower = "abcdefghijklmnopqrstuvwxyz";
  27. $number = "0123456789";
  28.  
  29. $seed_length = 0;
  30. $seed = '';
  31. $password = '';
  32.  
  33. if($use_upper === TRUE){
  34. $seed_length += 26;
  35. $seed .= $upper;
  36. }
  37. if($use_lower === TRUE){
  38. $seed_length += 26;
  39. $seed .= $lower;
  40. }
  41. if($use_number === TRUE){
  42. $seed_length += 10;
  43. $seed .= $number;
  44. }
  45. if(!empty($use_custom)){
  46. $seed_length +=strlen($use_custom);
  47. $seed .= $use_custom;
  48. }
  49. for($i = 1; $i <= $length; $i++){
  50. $password .= $seed{rand(0,$seed_length-1)};
  51. }
  52. return $password;
  53. } // End of generate
  54. } // End of Class Password
  55.  
  56. // ------------------------------------------------------------------------
  57.  
  58. /**
  59.  * With Special Characters
  60.  **/
  61. echo " With Special Characters: ";
  62. echo "------------------------- ";
  63. $password = new Password;
  64. echo ' 5: ' . $password->generate(array('length' => 5)) . " ";
  65. for ($i=10; $i <= 80; $i += 10) {
  66. $password = new Password;
  67. echo $i . ': ' . $password->generate(array('length' => $i)) . " ";
  68. }
  69.  
  70. // ------------------------------------------------------------------------
  71.  
  72. /**
  73.  * Without Special Characters
  74.  **/
  75. echo " Without Special Characters: ";
  76. echo "---------------------------- ";
  77. $password = new Password;
  78. echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . " ";
  79. for ($i=10; $i <= 80; $i += 10) {
  80. $password = new Password;
  81. echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . " ";
  82. }
  83.  
  84. // ------------------------------------------------------------------------
  85.  
  86. /**
  87.  * SHA1 Based
  88.  * These passwords will only contain lowercase letters from a to f and numbers from 0 to 9
  89.  **/
  90. $seed = sha1(uniqid(mt_rand(), true));
  91. $hash1 = sha1(uniqid($seed . mt_rand(), true));
  92. $hash2 = sha1(uniqid($seed . mt_rand(), true)).sha1(uniqid($hash1 . mt_rand(), true));
  93. echo " SHA1 Based: ";
  94. echo "------------ ";
  95. echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . " ";
  96. echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . " ";
  97. echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . " ";
  98. echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . " ";
  99. echo "40: ${hash1} ";
  100. echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . " ";
  101. echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . " ";
  102. echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . " ";
  103. echo "80: ${hash2} ";

以上是关于随机密码生成器Shell脚本-基于PHP类+SHA1的主要内容,如果未能解决你的问题,请参考以下文章

PHP 随机密码生成器PHP Shell脚本

Shell脚本批量创建用户并随机生成密码

案例四:Shell脚本生成随机密码

PowerShell脚本:随机密码生成器

生成8位随机密码脚本

利用Linux系统生成随机密码的9种方法