Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Quicky password sub

by halxd2 (Monk)
on May 02, 2000 at 16:26 UTC ( [id://9924]=perlcraft: print w/replies, xml ) Need Help??

   1: #Here's a quicky password generator sub. 
   2: #Great for account creation
   3: # Usage: my $Password = Pass();
   4: 
   5: sub Pass
   6:    {
   7:       
   8:       my $Num;
   9:       my $Other;
  10:       my $Pass = "";
  11:    
  12:       foreach $Num (1..8)
  13:       {
  14:          $Other = rand 122;
  15:          $Other =~ s/\..*//; #ditch the decimal part
  16:          while ( ($Other < 48) 
  17:             || (( $Other > 57) && ( $Other < 65))
  18:             || (( $Other > 90) && ( $Other < 97)))
  19:          {
  20:             $Other = rand 122;
  21:             $Other =~ s/\..*//; #ditch the decimal part
  22:          }
  23:          
  24:          $Pass = $Pass . chr $Other;
  25:       }
  26:       return($Pass); 
  27:    }

Replies are listed 'Best First'.
RE: Quicky password sub
by kayos (Sexton) on May 02, 2000 at 18:45 UTC

    This might be faster because it doesn't have all of the checks for the range of acceptable chars. It precalculates the acceptable chars and stores them in an array in a closure so it only has to calculate and store once. You can adjust the chars that you want acceptable by changing the the assignment in the closure.

    { my @password_chars = ( 0..9,A..Z,a..z ); # acceptable chars sub Pass { my $password = ''; for ( 1..8 ) { $password .= $password_chars[rand(@password_chars)]; } return $password; } }

    T.R. Fullhart, kayos@kayos.org

      It's 50% faster on my machine to do this:
      { my @password_chars = ( 0 .. 9, 'A' .. 'Z', 'a' .. 'z' ); my $num_char = @password_chars; sub Pass { my $password = ''; $password .= $password_chars[rand($num_char)] for (1 .. 8); return $password; } }

        You can also just say rand(@password_chars) and get rid of the $num_char variable entirely.

        Also, as long as your are hard coding the iteraction as 'for (1..8)' you can gain even more speed by just doing it like this: (forgive my shortening of password_chars)

        sub Pass { return $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)] . $p_c[rand(@p_c)]; }
        You could even take out the "return"!
        very very cool chromatic. It took me a minute to see the change that
        you made, but wow! cool.
RE: Quicky password sub
by PipTigger (Hermit) on May 04, 2000 at 22:02 UTC
    0: #A quick password generator which might suffice for account creatio +n 1: # Usage: my $Password = Pass(); 2: 3: sub Pass { 4: my $pas, $ran; 5: 6: foreach (1..8) { 7: $ran = int rand 62; #goods 48-57 65-90 97-122 = 10+26+26 8: if ($ran > 9) { $ran += 7; } # x57-64 9: if ($ran > 43) { $ran += 6; } # x90-96 A: $ran += 48; B: $pas .= chr $ran; C: } D: return($pas); E: }
    TTFN & Shalom.

    -PipTigger
    p.s. Mine sux compared to chromatic's & turnstep's... I'm lerning too =).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found