Simple complex password generator

These are really easy to write, but it's always handy to have your own. This one lives here in its cgi form.

  1. #! /usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. ## Let the browser know what we're sending it
  7. print "content-type: text/html\n\n";
  8.  
  9. ## Spew some HTML since we're not going to get away with plain text formatting
  10. print "<html>\n\t<head>";
  11. print "\t\t<title>Passwords!</title>";
  12. print "\t</head>";
  13. print "<body>\n\t<h1>Avi's Magical Password Generator</h1>";
  14.  
  15. ## Define two sets of data. The first is the lengths of password we want
  16. ## to produce, the second is the allowed characters. Each space-separated
  17. my $lengths="1 4 8 10 20 30 50 80 100";
  18. my $characters="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0 ! $ % ^ & _ + = : ; @ # ~ , . ? ";
  19.  
  20. ## Split the above by space into arrays
  21. my @chars=split(/ /, $characters);
  22. my @lens=split(/ /, $lengths);
  23.  
  24. ## Work out how many characters we are allowed, so that when we
  25. ## come to pick one at random we can use this number as the maximum.
  26. my $chars_count = @chars;
  27.  
  28. ## Spew out some html for pretty formatting:
  29. print "<table>\n";
  30. print "\t<tr><td>length</td><td></td></tr>";
  31.  
  32.  
  33. ## The loop!
  34. foreach (@lens){
  35. my $length=$_;
  36. my $count;
  37. ## The first cell of the row, containing the length:
  38. print "\n\t<tr><td>".$_."</td><td>";
  39. for ($count = 1; $count < = $length; $count++){
  40. ## Working backwards, $chars_count is the above-defined
  41. ## number of characters we have to play with,
  42. ## rand($chars_count) picks a random number between 0 and
  43. ## $chars_count, and int(rand($chars_count)) makes sure
  44. ## it's an integer. This in the square brackets after $chars
  45. ## means we're picking a random element out of the array
  46. ## @chars, which is effectively picking a random character
  47. ## out of the list of allowed ones.
  48. print $chars[int(rand($chars_count))];
  49. }
  50. print "</td></td></tr>\t\t\n";
  51. }
  52. print "</table>\n\n";
  53.  
  54.  
  55. print "<a href=./password.txt>sauce</a> <a href=..>home</a>";
  56. print "</body>";</html>