Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file

by marto (Cardinal)
on Sep 26, 2018 at 11:13 UTC ( #1223046=note: print w/replies, xml ) Need Help??


in reply to Re^4: with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file
in thread with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file

You need to take a step back and think about what you're doing, rather than just copying/pasting things and hoping for the best. You've simply printed a second hash, using the string 'username' combined with the hash you got the first time round. Why would you do this? When reading in the user input you want to generate the hash then compare it to the one stored in your text file. MD5, Digest::MD5.

  • Comment on Re^5: with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file

Replies are listed 'Best First'.
Re^6: with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file
by theravadamonk (Scribe) on Sep 27, 2018 at 05:09 UTC

    "When reading in the user input you want to generate the hash then compare it to the one stored in your text file."

    Thanks for the above explanation...

    Now, Let's break this statement..

    first "When reading in the user input"

    In this case, user inputs USERNAME as "username" and PASSWORD as "SamplePassword".

    in my login.cgi file, below 2 lines carry them.

    my $var_username = param( "USERNAME" ); my $var_password = param( "PASSWORD" );

    secondly "you want to generate the hash"

    now, $var_password is PLAIN text and NAKED. Now, I will have to use "use Digest::MD5;" to encrypt it.

    this is the code for it.

    my $encrypted_password = Digest::MD5::md5_hex("$var_username" . "$var_ +password"); #print "encrypted_password : $encrypted_password \n"; # This shows the + encrypted password.

    Thirdly, "then compare it to the one stored in your text file."

    Now, this encrypted password contains the /tmp/password.txt file along with username in this way.

    username,c0075ad4e26ec3dee225ccb6387b0b77

    Now, Let's apply it to the code. Now I need $encrypted_password instead of $var_password.

    ( $var_password is PLAIN and not in /tmp/password.txt file. but $encrypted_password is stored in /tmp/password.txt )

    here I am gonna apply it.

    if ( ( $var_username eq $username ) && ( $encrypted_password eq $pass +word ) ) { print "$var_username, $encrypted_password <br>"; # I will remove th +is in real world print "Permission has been granted <br>"; print "<META HTTP-EQUIV=refresh CONTENT=\"$t;URL=$url\">\n"; $didmsg=1; last; }

    Now, It works. thanks a LOT. here's the full code. If there are any issues, Pls come back.

    my login.cgi

    #!/usr/bin/perl use CGI qw(:standard); use strict; use warnings; use Digest::MD5; my $var_username = param( "USERNAME" ); my $var_password = param( "PASSWORD" ); my $encrypted_password = Digest::MD5::md5_hex("$var_username" . "$var_ +password"); my $url="http://host.redirectiondomain.com:9999/"; my $t=1; # time until redirect activates print "Content-Type: text/html; charset=utf-8\n\n"; open ( FILE, "/tmp/password.txt" ) || die "The file could not be opene +d"; my $didmsg=0; while ( my $line = <FILE> ) { chomp $line; ( my $username, my $password ) = split( ",", $line ); if ( ( $var_username eq $username ) && ( $encrypted_password eq $pa +ssword ) ) { print "$var_username, $encrypted_password <br>"; # I will remove th +is in real world print "Permission has been granted <br>"; print "<META HTTP-EQUIV=refresh CONTENT=\"$t;URL=$url\">\n"; $didmsg=1; last; } elsif ( ( $var_username eq $username ) && ( $encrypted_password ne +$password ) ) { print "$var_username, $encrypted_password <br>"; # I will remove th +is in real world print "You entered an invalid password. <br>"; print "Access has been denied. <br>"; $didmsg=1; last; } } close( FILE ); unless($didmsg) { print "$var_username, $encrypted_password <br>"; # I will remove th +is in real world print "You entered an invalid username. <br>"; print "Access has been denied. <br>"; }

      " If there are any issues, Pls come back."

      Remove the part about plain text and my first reply and it's still valid.

        "Remove the part about plain text and my first reply and it's still valid."

        What is this "Remove the part about plain text"? What do you actually want me to do?

        Do you want me to remove the below line in my message?

        "$var_password is PLAIN text and NAKED."

        or

        your "first reply" also consists of "nor is having passwords in plain text."

        If it is a must, You may explain me. I feel sorry, If I have not understood your question.

        you made my day easier...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1223046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-01-16 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (49 votes). Check out past polls.

    Notices?