in reply to Creating random passwords for users.

Eew. For each password, you tie a list of words using Tie::File at least twice - only to extract a single word each time. In order to do so, you have to read the entire file (if only to determine the number of lines). And you recurse if the word you pick is too long. While you'll save some memory, this isn't going to be efficient. I'd write select_a_word as:
my @words; INIT { open my $wh, "<", "/usr/share/dict/linux.words" or die; @words = grep {6 > length} <$wh>; close $wh or die; } sub select_a_word {lc $words[rand @words]}
But then, I wouldn't use your snippet, if only for the fact it can generate passwords not longer than 5 characters. Your number of possible salts is also low, 676 instead of the 4096 possibilities most systems allow for.

Finally, I'd use 'crypt' to generate both sets of passwords, as my system knows whether to use DES or MD5 based on the salt being used. (DES takes a 2 character salt, MD5 an 8 character salt, preceeded with '$1$', and (optionally) followed by a '$', giving 11 or 12 characters to pass into 'crypt'). But not every system will do so.

Perl --((8:>*

Replies are listed 'Best First'.
Re^2: Creating random passwords for users.
by blue_cowdawg (Monsignor) on Nov 04, 2005 at 13:22 UTC
        Eew. For each password, you tie a list of words using Tie::File at least twice - only to extract a single word each time. In order to do so, you have to read the entire file (if only to determine the number of lines). And you recurse if the word you pick is too long.

    Please Note: I did mention something about that as being one of the weaknesses of the design. I would certainly come up with a better way if I were to use this in a "more permanent" fashion. I'm not sure that slurping all the files into memory is the answer either.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg