mypasswd

pll at lanminds.com pll at lanminds.com
Mon Sep 30 12:46:58 EDT 2002


Hi all,

I was digging through my ~/bin dir this morning, and came across this 
gem which I wrote quite some time ago.

Basically, it's a passwd generating program similar to /bin/passwd, 
except it's written in perl and doesn't actually operate on the
/etc/passwd file.  It also has a few features which I evidently 
needed at some point in time :)

Here's a rough list of what it can do:

	- take a clear text string and encypt it
	- encrypt a clear text string X number of times
	- encrypt a clear text string using a specific 2 digit salt
	- take a list of clear text strings and encrypt them

The list feature can either be a named file, or read from STDIN.

The code is pretty straightforward, but feel free to ask if I did 
something you don't understand.

Don't ask me *why* I wrote this, or when.  I'm sure at the time I had 
an itch to scratch :)

You could in theory use this as a password generator for a web page,
but it would likely need some work. Also, it doesn't yet do md5 based 
passwords, though, that's not hard, using the MD5 perl module.

This is released under the GPL, and please criticize/suggest better 
ways of doing this.

I hope someone finds this useful!

Seeya,
Paul

-------------- next part --------------
#!/usr/bin/perl

use Getopt::Long;		# use the getopt routine

# parse command line opts
$result = GetOptions ('h',	# show help
		      'f=s',	# take passwds from a file
		      'c=i',	# create # of passwds specified
		      'p=s',	# encrypt *this* string
		      's=s');   # use *this* salt

if ($opt_h) {&usage;}			 # They need some help

if ($opt_f) {		# Reading from a file

  open (PWFILE, "<$opt_f") || die "$!\n";
  @pwfile = <PWFILE>;
  chomp (@pwfile);
  close (PWFILE);

  foreach $passwd (@pwfile) {
    encode ($passwd) unless ($passwd =~ /^$/); # don't encrypt a blank line
  }

} elsif ($opt_p) {	# Specifying on the command line

    encode ($opt_p);

} else {		# Entering interactively and going to ask twice

  system "stty -echo";

  $tries = 0;
  $okay = 0;
  while  (! $okay) {
    $okay = 1;
    print "Enter password: ";
    chomp($password1 = <>);
    print "\n";

    print "Enter password again: ";
    chomp($password2 = <>);
    print "\n";
    if  (($password1 ne $password2) and ($tries != 2)){
      message ("Error: Password mismatch");
      $okay = 0;
    } elsif ((length ($password1) < '6') and ($tries != 2)) {
      message ("Error: Password too short");
      $okay = 0;
    }
    if ($tries == 2) {
      message ("Error: Too many tries","1");
    }
    $tries++;

  }

  system "stty echo";
  encode ($_);
}

sub encode {

  my ($passwd) = shift;
  my ($count);
  my ($salts) = $opt_s;

  my (@s) = (a..z, A..Z, 0..9, ".", "/");	# Valid character sets for salt

  # create a random salt, gotta be 2 characters, store it
  # in an array (@salts), then use them to create the encrypted passwords.
  # Finally, print them out.

  if ($opt_c) {

    for ($count = 0; $count < $opt_c; $count++) {
     $salts = $s[rand(@s)] . $s[rand(@s)] unless ($opt_s);
      my ($encrypted) = crypt($passwd, $salts);
      print ("$encrypted\n");
    }

  } else {

    $salts = $s[rand(@s)] . $s[rand(@s)] unless ($opt_s);
    $encrypted = crypt($passwd, $salts);
    print ("$encrypted\n");		

  }

}

sub message {

  my ($message) = shift;
  my ($exit) = shift;
  if ($exit) { 
    system ("stty echo");
    die "$message\n";
  } else {
    print STDERR("$message\n");
  }
}


sub usage {

# get the basename of the program being run.  This should work fine
# and I want this to be fast, so I don't want to use File::Basename
  ($prog = $0) =~ s/.*\/(\w+)/$1/;

# Here's the old HERE Doc thingy.  I like this better than a bunch of prints.
# with prints you need to worry that the tabs won't line up, besides, this is
# easier to edit ;)
# Oh yeah, since this is technically an error, send it where it's supposed
# to go like a good little program :)
print STDERR <<END;

USAGE: $prog [ h | [ -f <file> | -p <cleartext pw> ] [-c \# ] [-s <salt>] ]

$prog creates encrypted passwords based on cleartext input

  -h	This message
  -c	How many passwords you want to create
  -f	Use a file containing all the cleartext passwords
  -p	Encrypt this string
  -s	Use this string as the 2 digit salt

END

exit;
}
-------------- next part --------------

Seeya,
Paul
--
	It may look like I'm just sitting here doing nothing,
   but I'm really actively waiting for all my problems to go away.

	 If you're not having fun, you're not doing it right!


More information about the gnhlug-discuss mailing list