Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Removing similar characters

by Not_a_Number (Parson)
on Sep 02, 2011 at 13:09 UTC ( #923841=note: print w/ replies, xml ) Need Help??


in reply to Removing similar characters

A bit late, perhaps, but:

1) I agree that overgenerating and then filtering out unwanted strings is a Bad Idea (too redolent of a bogosort for my liking :).

2) The code below uses the four character classes implicit in the OP.

use strict; use warnings; my $password_length = 8 + rand 3; # Or whatever (here 8-10 chars) my @classes = ( qw/ upper lower digit punct / ); my @pattern; for ( 1 .. $password_length ) { my @tmp = grep { $_ ne last2( \@pattern ) } @classes; push @pattern, $tmp[ rand @tmp ]; } my %chars; for my $class ( @classes ) { @{ $chars{$class} } = map { chr =~ /[[:$class:]]/g } 0 .. 127; } my $password = join '', map ${ $chars{$_} }[ rand @{ $chars{$_} } ], @ +pattern; print $password; sub last2 { my $to_check = shift; return 'ok' if @{ $to_check } < 2; return $to_check->[ -1 ] if $to_check->[ -1 ] eq $to_check->[ -2 ]; return 'ok'; }

That said, if I were to be issued a password like |V$w`'N#^8, I wouldn't hesitate to write it on a post-it and stick it on my monitor!


Comment on Re: Removing similar characters
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://923841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-08-22 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (147 votes), past polls