Random Password Generator Shell Script - PHP Class + SHA1 based


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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 "\nWith Special Characters: \n";
  62. echo "-------------------------\n";
  63. $password = new Password;
  64. echo ' 5: ' . $password->generate(array('length' => 5)) . "\n";
  65. for ($i=10; $i <= 80; $i += 10) {
  66. $password = new Password;
  67. echo $i . ': ' . $password->generate(array('length' => $i)) . "\n";
  68. }
  69.  
  70. // ------------------------------------------------------------------------
  71.  
  72. /**
  73.  * Without Special Characters
  74.  **/
  75. echo "\nWithout Special Characters: \n";
  76. echo "----------------------------\n";
  77. $password = new Password;
  78. echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . "\n";
  79. for ($i=10; $i <= 80; $i += 10) {
  80. $password = new Password;
  81. echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . "\n";
  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 "\nSHA1 Based: \n";
  94. echo "------------\n";
  95. echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . "\n";
  96. echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . "\n";
  97. echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . "\n";
  98. echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . "\n";
  99. echo "40: ${hash1}\n";
  100. echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . "\n";
  101. echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . "\n";
  102. echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . "\n";
  103. echo "80: ${hash2}\n";

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.