Ignore:
Timestamp:
09/19/12 17:22:52 (12 years ago)
Author:
nanardon
Message:

add tools to generate random password

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r1124 r1151  
    88use LATMOS::Accounts::Log; 
    99use File::Temp qw(tempfile); 
     10use Crypt::Cracklib; 
    1011 
    1112our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    247248} 
    248249 
     250=head2 genpassword 
     251 
     252=cut 
     253 
     254sub genpassword { 
     255    my (%options) = @_; 
     256    my @non_alpha = (qw$; : / ! ( ) [ ] { } + = @ - " ' % * & . ? < >$, ',', '$'); 
     257    my @letters = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); 
     258 
     259    my @consonants = qw(b d f g j k l m n r s t v x z ch); 
     260    my @vowels = qw(a e i o u ou oi io ia iu); 
     261 
     262    $options{length} ||= 6 + int(rand(3)); 
     263 
     264    while (1) { 
     265        if ($options{syllables}) { 
     266            $options{length} = int($options{length} / 2); 
     267            $options{length} = 3 if ($options{length} < 3); 
     268        } else { 
     269            $options{length} = 6 if ($options{length} < 6); 
     270        } 
     271 
     272        my @chars; 
     273 
     274        if ($options{nonalpha}) { 
     275            for(0 .. (0 + int(rand(3)))) { 
     276                push(@chars, (@non_alpha[rand(scalar(@non_alpha))])); 
     277            } 
     278        } 
     279 
     280        foreach (1 .. ($options{length} - scalar(@chars))) { 
     281            if ($options{syllables}) { 
     282                my $c = @consonants[rand(scalar(@consonants))]; 
     283                my $v = @vowels[rand(scalar(@vowels))]; 
     284                push(@chars, "$c$v"); 
     285            } else { 
     286                push(@chars, (@letters[rand(scalar(@letters))])); 
     287            } 
     288        } 
     289 
     290 
     291        my $pass = join('', sort { rand() <=> rand() } @chars); 
     292        if (length($pass) >= 6 && fascist_check($pass) eq 'ok') { 
     293            return $pass; 
     294        } 
     295    } 
     296} 
     297 
    2492981; 
    250299 
Note: See TracChangeset for help on using the changeset viewer.