Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Create encrypted passwords

by Kozz (Friar)
on Mar 25, 2000 at 04:05 UTC ( #6136=perlcraft: print w/ replies, xml ) Need Help??

   1: Good for creating passwords for .htpasswd files or for any other *nix password file:
   2: 
   3: <CODE>
   4: sub mkpasswd{
   5:   my($plaintextpass)=@_;
   6:   my($salt1, $salt, $encrypted);
   7: 
   8:   $salt1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./'; 
   9:   $salt = substr($salt1,rand(length($salt1)),1);
  10:   $salt .= substr($salt1,rand(length($salt1)),1);
  11:   $encrypted = crypt($plaintextpass, $salt);
  12: 
  13:   return $encrypted;
  14: }
  15: </CODE>

Comment on Create encrypted passwords
Download Code
RE: Create encrypted passwords
by Anonymous Monk on Mar 27, 2000 at 13:52 UTC
    Line 9 assigns a character to $salt, so when $salt is used in line 10, it will pick a random character from the 1 character $salt, which means $salt will always be two repeated characters.
RE: Create encrypted passwords
by Anonymous Monk on Mar 28, 2000 at 08:08 UTC
    Better so: @chars=(a..z,A..Z,0..9,'.','/'); $salt=$charsrand(64).$charsrand(64); Zak
      Try again *sigh*...
      . . . @chars=(A..Z,a..z,0..9,'.','/'); $salt=$chars[rand(64)].$chars[rand(64)]; . . .

      Zak

      Better still:

      $salt = join '', ('a'..'z','A'..'Z',0..9,'.','/')[rand 64, rand 64] ;

      -Ducky

      Update: Ok, took me a year to come back and fix this, but it's fixed the way I'd do it today. =/

        What if you want to expand the valid characters someday?
        @chars=(a..z,A..Z,0..9,'.','/'); $salt= join '', @chars[rand(@chars), rand(@chars)];
        or even
        my $elem = @chars; $salt = join '', @chars[rand($elem), rand($elem)];
        That doesn't work. You're taking an array slice (incorrectly), then assigning it to a scalar variable. You want something like this:
        my @chars = ('a'..'z', 'A'..'Z', 0..9, '.', '/'); my $salt = join '', @chars[rand @chars, rand @chars];
        Note the "@" before "chars"--that denotes an array slice. And since we're taking an array slice and want to end up with a scalar, we need to do a join.
RE: Create encrypted passwords
by IndyZ (Friar) on Apr 19, 2000 at 06:46 UTC
    From a crypto point of view, it is <BLINK>very</BLINK> important to remember that on many systems, the rand function may not be truly random.
    You have been warned.

    Brian
Re: Create encrypted passwords
by NodeReaper (Curate) on Oct 10, 2004 at 05:50 UTC
    This node was taken out by the NodeReaper on Sun Oct 10 07:50:09 2004 (EST)
    Reason: shenme Delete - a graffito on our institutional walls

    For more information on this node visit: this

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-12-26 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls