#!/usr/bin/perl -w


#####################################################################################
# This program is not guaranteed to work at all, and by using this program you      #
# release the author of any an all liability.                                       #
# You may use this program so long as this notice and disclaimer remain intact.     #
#                                                                                   #
# This program will create a hash (SHA1, MD5, etc) of a plain text password string. #
#                                                                                   #
# Written by Bret Jordan                                                            #
# bret.jordan@utah.edu                                                              #
# http://utahgeeks.sourceforge.net                                                  #
#                                                                                   #
#####################################################################################
my $sVersion = "0.01";


use strict;
use Digest::MD5;
use Digest::SHA1;
use Term::ReadKey;
use Getopt::Long;




# ---------------------------------------------------------
# Process Command Line Options
# ---------------------------------------------------------
my %options = &get_options();
&print_syntax if (defined $options{h} || defined $options{help});

my $sClearPassword;
if ($options{p})
{
    $sClearPassword = $options{p};
}
else
{
    $sClearPassword = &get_pwd();
}


# ---------------------------------------------------------
# Create Encrypted Password
# ---------------------------------------------------------
my $ctx;
if (lc($options{d}) eq "md5") {
  $ctx = Digest::MD5->new;
}
elsif (lc($options{d}) eq "sha1") {
  $ctx = Digest::SHA1->new;
}

$ctx->add($sClearPassword);
  
if (lc($options{e}) eq "bin") {
  print $ctx->digest;
}
elsif (lc($options{e}) eq "hex") {
  print $ctx->hexdigest;
}
elsif (lc($options{e}) eq "b64") {
  print $ctx->b64digest;
}




# ---------------------------------------------------------
# Subroutines
# ---------------------------------------------------------

sub get_pwd {
    my ($sPassword, $sPasswordVerify);

    do 
    {
        print "Password: ";
        ReadMode('noecho');
        chomp ($sPassword = ReadLine(0));
        print "\n";
        ReadMode('normal');
    
        print "Verify Password: ";
        ReadMode('noecho');
        chomp ($sPasswordVerify = ReadLine(0));
        print "\n";
        ReadMode('normal');
        print "Passwords do not match!\n" if ($sPassword ne $sPasswordVerify);
    } 
    while ($sPassword ne $sPasswordVerify);

    return $sPassword;
}


sub get_options {
    my %flags;

    if (@ARGV > 0)
    {
        # allow flags denoted by "-" or "--"
        Getopt::Long::Configure("prefix_pattern=(--|-)");
        GetOptions (\%flags, qw(d=s e=s p=s h|? help));
    }

    # Set defaults to SHA1 and Base64
    if (!defined $flags{d}) {$flags{d} = "sha1";}
    if (!defined $flags{e}) {$flags{e} = "b64";}
    
    return %flags;
}


sub print_syntax {
    my ($sScript) = ($0 =~ /([^\\\/]*?)$/ );
    print <<EOT;
Create encrypted password.
Version: $sVersion

Syntax:
    perl $sScript [-d Digest] [-e EncodingType] [-p FileName] [-h]
    -d           Digest (md5 or sha1), sha1 is the default.
    -e           Encoding (bin=Binary, hex=Hex, b64=Base64) b64 is the default.
    -p           Password.
    -h           This help menu.

EOT
exit();
}









SourceForge.net Logo