There would be a lot less code to do the same thing if you start with a string that has all characters available for use in passwords, then remove classes of characters according to the given options:
(update: added a couple of missing "+" to the regexen)my $pswd_chars = join( '', map {chr} ( 0x21 .. 0x7e )); $pswd_chars =~ s/\d+// if ( $numbers ); $pswd_chars =~ s/[A-Z]+// if ( $uppercase ); $pswd_chars =~ s/[a-z]+// if ( $lowercase ); $pswd_chars =~ s/[_\W]+//g if ( $symbols ); if ( length( $pswd_chars ) == 0 ) { push @errmsg, "\n *** You eliminated all possible characters."; }
Also, if someone wants to use this to create passwords to be handed out to clients (i.e. via email or web transactions), another useful option would be to eliminate confusable characters:
As for random selection based on the remaining characters:$pswd_chars =~ tr/1Il0O//d if ( $confusable );
Last suggestion: definitely check out POD, and Pod::Usage -- you'll fall in love with that.sub GenPass { my ( $pswd_chars, $pswd_len ) = @_; my $limit = length( $pswd_chars ); my $pswd = ''; for ( 0..$pswd_len-1 ) { $pswd .= substr( $pswd_chars, rand( $limit ), 1 ); } return $pswd; }
In reply to Re: genpass Password Generator
by graff
in thread genpass Password Generator
by munkyeetr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |