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

[SOLVED]- FOUND OTHER DOCUMENTATION Encrypting a password to MD5

by jaffinito43 (Initiate)
on Nov 13, 2012 at 00:42 UTC ( #1003530=perlquestion: print w/replies, xml ) Need Help??
jaffinito43 has asked for the wisdom of the Perl Monks concerning the following question:

So I have a password file where users passwords are stored as the MD5 hash of "changeme"..(4cb9c8a8048fd02294477fcb1a41191a)... My script, which asks for the username and password, accepts the password as plain text but I have no idea how to calculate the MD5 hash to compare it to the password file. It seems like it's too complicated to try to write an algorithm for so does anyone know of any ideas? Thanks!

# CSC 310 Project # 3 #opening passwd file open (PSWD, '<', 'passwd.txt'); #getting username and password #converting username to lowercase if anything is entered in CAPS print "Please enter your username: "; chomp($userN = <STDIN>); $username = lc($userN); print "Please enter your password: "; #hiding password system ("stty -echo"); chomp($passwd = <STDIN>); $matchCount = 1; #reading passwd.txt and assigning values while ($lines = <PSWD>){ ($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $li +nes; #checking username entered vs that in the passwd file if ($username eq $user){ print "\nChecking username... MATCH\n"; #keeps track if username matches or not $matchCount += 1; #checking password entered vs that in the passwd file if ($passwd eq $pswd){ print "Checking password... MATCH\n\n"; $matchCount -= 2; } else{ print "Password does not match!\n"; } last; } } # if matchcount did not change, username did not match killing the pro +gram if ($matchCount != 0){ die ("\nEither the username or password did not match.\n"); } ......

Right now, the script checks the plain text of the file (I changed the passwords stored in the password file) and works great, but the MD5 hash is going to throw me off. Any help is appreciated.

Replies are listed 'Best First'.
Re: [SOLVED]- FOUND OTHER DOCUMENTATION Encrypting a password to MD5
by davido (Archbishop) on Nov 13, 2012 at 01:39 UTC
Re: [SOLVED]- FOUND OTHER DOCUMENTATION Encrypting a password to MD5
by zentara (Archbishop) on Nov 13, 2012 at 12:16 UTC
    Is your problem solved? You never did show the code that solved your problem. If not, see encryption confusion, you need to get the salt values, on linux it is in /etc/shadow.

    This super simple example shows the basics:

    #!/usr/bin/perl use warnings; use strict; print "Standard crypt output\n"; print crypt("password","sa"), "\n"; #glibc's implementation that takes care of this. Since perl on # Linux uses glibc, you get this automagically. When the salt looks li +ke # $1$...., it will encrypt the password using MD5. e.g. print "MD5 crypt output\n"; print crypt("password","\$1the_salt\$"), "\n"; print "SHA crypt output\n"; print crypt("password","\$2the_salt\$"), "\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003530]
Approved by rovf
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-06-18 14:25 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.