Posted By

DAT404 on 12/05/10


Tagged

password random generator perl


Versions (?)

Random password generator


 / Published in: Perl
 

Usage: perl script.pl length

  1. #!/usr/bin/perl
  2. # Script: Random password generator
  3. # Fri 03 Dec 2010 01:30:24 PM CET
  4. #use strict;
  5.  
  6. # List of chars. Modify this if you need to add more, like simbols, etc...
  7. my @chars = qw( a b c d e f g h i j k l m n o p q r s t
  8. u v w x y z A B C D E F G H I J K L M N
  9. O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7
  10. 8 9 );
  11.  
  12. # The array of the password.
  13. my @password = ("");
  14.  
  15. # Select a random char from the array @chars and introduced into the array @password.
  16. sub pass_gen {
  17. my $rand = int(rand(@chars));
  18. my $add_char = $chars[$rand];
  19. push(@password, $add_char);
  20. }
  21.  
  22. # The length of the password. If none is supplied externally, given the value of 8.
  23. my $length = shift || 8;
  24.  
  25. # Call the function pass_gen X (the value stored in $lenght) times.
  26. for (my $x = 1; $x <= $length; $x++) {
  27. &pass_gen();
  28. }
  29.  
  30. # Print the password without spaces.
  31. print "Here is your password:\n";
  32. print @password = grep { "$_" } @password, "\n";
  33.  
  34. #End of file

Report this snippet  

You need to login to post a comment.