oldie but goldie solution to generate random salts, or whatever strings etc + a simple example of password generator
#!/usr/bin/perl ###### # DES/MD5 password generator # AltBlue 1999/02/10 ###### # Usage: # ./mkpass [<password>] # - if no password supplied, a random password will be picked up. ######### use strict; use Crypt::PasswdMD5; my $password = defined $ARGV[0]?$ARGV[0]:random_hash(8,32); print "CLEAR:\t", $password, $/, "DES:\t", crypt($password,random_hash(2,2)), $/, "MD5:\t", unix_md5_crypt($password,random_hash(8,8)), $/; sub random_hash { my ($ash,$ml) = ('',((defined$_[0]&&$_[0]>0)?$_[0]:4)); my $Ml=(defined$_[1]&&$_[1]>=$ml)?$_[1]:$ml+5; $ash .= ('.','/',0..9,'A'..'Z','a'..'z')[rand 64] foreach(1..($ml+int(rand $Ml-$ml))); $ash; }

Replies are listed 'Best First'.
Re: password (and random string/salt) generator
by Preceptor (Deacon) on Sep 19, 2002 at 13:12 UTC
    Lovely, and very handy. But I'm somewhat confused as to what the parameters are in the subroutine...
    the second is the 'length' of the return string, that's obvious, but what's the first?
    Update: It always helps to read code at some point _other_ than first thing in the morning. Param 1 is min length, param 2 is max length.
    --
    It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.
Re: password (and random string/salt) generator
by Anonymous Monk on Jun 14, 2002 at 19:33 UTC
    gtrqyrey