473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sha1() passwd in mysql help... (beginner)

Dear group,

For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with

$passwd = sha1($_REQUEST['passwd']);

I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.

Mar 26 '07 #1
8 5601
sathyashrayan wrote:
Dear group,

For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with

$passwd = sha1($_REQUEST['passwd']);

I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.
How?
Compare them of course.
The fact that the password is encrypted doesn't make it something else than
a string of bits.

So:
supose you have a table with userid and sha1_passwd:

$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd = '".$passwd."'); ";

Execute it and see if it has a result. If not, no good password, if so, you
have the userid.

Regards,
Erwin Moller
Mar 26 '07 #2
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
sathyashrayan wrote:
Dear group,
For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with
$passwd = sha1($_REQUEST['passwd']);
I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.

How?
Compare them of course.
The fact that the password is encrypted doesn't make it something else than
a string of bits.

So:
supose you have a table with userid and sha1_passwd:

$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd = '".$passwd."'); ";

Execute it and see if it has a result. If not, no good password, if so, you
have the userid.

Regards,
Erwin Moller
This way?

<?php
$sha = sha1("sathya"); /*$sha to be inserted in db*/

$new = $sha; /*save the passwd localy*/

if($new === $sha)
echo "correct";
else
echo "wrong";
?>
Mar 26 '07 #3
On 26 Mar, 13:29, "sathyashra yan" <asm_f...@yahoo .co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller

<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
sathyashrayan wrote:
Dear group,
For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with
$passwd = sha1($_REQUEST['passwd']);
I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.
How?
Compare them of course.
The fact that the password is encrypted doesn't make it something else than
a string of bits.
So:
supose you have a table with userid and sha1_passwd:
$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd = '".$passwd."'); ";
Execute it and see if it has a result. If not, no good password, if so, you
have the userid.
Regards,
Erwin Moller

This way?

<?php
$sha = sha1("sathya"); /*$sha to be inserted in db*/

$new = $sha; /*save the passwd localy*/

if($new === $sha)
echo "correct";
else
echo "wrong";
?>
erwin just gave your answer.

registration stage
get user's password at registration - you should do this securely
using SSL.
hash and store in database = sha1(users_plai ntext_password)

login stage
1. create a random string and store in session on server,
2. send login form with username, password, and random string
3. when user enters password, set password field to
sha1( sha1(users_plai ntext_password) +random string ), and post form
auth stage
server computes sha1( users_hashed_pa ssword_in_datab ase +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_pa ssword_in_datab ase + $_SESSION['random_string'] )

then OK, else not.

Mar 26 '07 #4
On 26 Mar, 15:35, "shimmyshac k" <matt.fa...@gma il.comwrote:
On 26 Mar, 13:29, "sathyashra yan" <asm_f...@yahoo .co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
sathyashrayan wrote:
Dear group,
For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with
$passwd = sha1($_REQUEST['passwd']);
I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.
How?
Compare them of course.
The fact that the password is encrypted doesn't make it something else than
a string of bits.
So:
supose you have a table with userid and sha1_passwd:
$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd = '".$passwd."'); ";
Execute it and see if it has a result. If not, no good password, if so, you
have the userid.
Regards,
Erwin Moller
This way?
<?php
$sha = sha1("sathya"); /*$sha to be inserted in db*/
$new = $sha; /*save the passwd localy*/
if($new === $sha)
echo "correct";
else
echo "wrong";
?>

erwin just gave your answer.

registration stage
get user's password at registration - you should do this securely
using SSL.
hash and store in database = sha1(users_plai ntext_password)

login stage
1. create a random string and store in session on server,
2. send login form with username, password, and random string
3. when user enters password, set password field to
sha1( sha1(users_plai ntext_password) +random string ), and post form

auth stage
server computes sha1( users_hashed_pa ssword_in_datab ase +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_pa ssword_in_datab ase + $_SESSION['random_string'] )

then OK, else not.
Sorry Erwin. I should add, that I was assuming that "The password
field is encrypted with" meant that he initially used javascript to
hash the password client side, however on rereading - it doesnt appear
to be the case, so in this case job done, he should just run your code!

Mar 26 '07 #5
On Mar 26, 4:39 pm, "sathyashra yan" <asm_f...@yahoo .co.ukwrote:
Dear group,

For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with

$passwd = sha1($_REQUEST['passwd']);

I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.
Thanks for all the help.

Mar 26 '07 #6
Erwin Moller kirjoitti:
sathyashrayan wrote:
> Dear group,

For a log-in page I have created a mysql db and user registers
with a user name and password. The password field is encrypted with

$passwd = sha1($_REQUEST['passwd']);

I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that. Any
help is appreciated. Thanks in advance.

How?
Compare them of course.
The fact that the password is encrypted doesn't make it something else than
a string of bits.

So:
supose you have a table with userid and sha1_passwd:

$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd = '".$passwd."'); ";
I'd select first the row that matches username and then compare the
password of that row to the sha'd password.

The problem with your method is that two users having the same password
(say "123abc" or "password") can collide. Usernames should be unique,
passwords shouldn't. (Furthermore, if a user tries to set a password and
system reports that it's taken, it opens an unwanted door...)

--
Ra*********@gma il.com
"Olemme apinoiden planeetalla."
Mar 26 '07 #7
shimmyshack wrote:
On 26 Mar, 15:35, "shimmyshac k" <matt.fa...@gma il.comwrote:
>On 26 Mar, 13:29, "sathyashra yan" <asm_f...@yahoo .co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
sathyashrayan wrote:
Dear group,
For a log-in page I have created a mysql db and user
registers
with a user name and password. The password field is encrypted with
$passwd = sha1($_REQUEST['passwd']);
I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that.
Any help is appreciated. Thanks in advance.
How?
Compare them of course.
The fact that the password is encrypted doesn't make it something
else than a string of bits.
So:
supose you have a table with userid and sha1_passwd:
$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd =
'".$passwd."'); ";
Execute it and see if it has a result. If not, no good password, if
so, you have the userid.
Regards,
Erwin Moller
This way?
<?php
$sha = sha1("sathya"); /*$sha to be inserted in db*/
$new = $sha; /*save the passwd localy*/
if($new === $sha)
echo "correct";
else
echo "wrong";
?>

erwin just gave your answer.

registration stage
get user's password at registration - you should do this securely
using SSL.
hash and store in database = sha1(users_plai ntext_password)

login stage
1. create a random string and store in session on server,
2. send login form with username, password, and random string
3. when user enters password, set password field to
sha1( sha1(users_plai ntext_password) +random string ), and post form

auth stage
server computes sha1( users_hashed_pa ssword_in_datab ase +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_pa ssword_in_datab ase + $_SESSION['random_string'] )

then OK, else not.

Sorry Erwin.
Hey! Don't appologize!
Your explanation was a lot better and clearer than my short vague response.
:-)
I didn't even mention a random string. ;-)

Regards,
Erwin

I should add, that I was assuming that "The password
field is encrypted with" meant that he initially used javascript to
hash the password client side, however on rereading - it doesnt appear
to be the case, so in this case job done, he should just run your code!
Mar 27 '07 #8
On 27 Mar, 13:54, Erwin Moller
<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
shimmyshack wrote:
On 26 Mar, 15:35, "shimmyshac k" <matt.fa...@gma il.comwrote:
On 26 Mar, 13:29, "sathyashra yan" <asm_f...@yahoo .co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
sathyashrayan wrote:
Dear group,
For a log-in page I have created a mysql db and user
registers
with a user name and password. The password field is encrypted with
$passwd = sha1($_REQUEST['passwd']);
I insert the $passwd in mysql_insert. The password gets
encrypted and stored in mysql. Now I want to check if the user has
entered the correct password when he logs in. How can I do that.
Any help is appreciated. Thanks in advance.
How?
Compare them of course.
The fact that the password is encrypted doesn't make it something
else than a string of bits.
So:
supose you have a table with userid and sha1_passwd:
$passwd = sha1($_REQUEST['passwd']);
$SQL = "SELECT userid FROM tblusers where (sha1_passwd =
'".$passwd."'); ";
Execute it and see if it has a result. If not, no good password, if
so, you have the userid.
Regards,
Erwin Moller
This way?
<?php
$sha = sha1("sathya"); /*$sha to be inserted in db*/
$new = $sha; /*save the passwd localy*/
if($new === $sha)
echo "correct";
else
echo "wrong";
?>
erwin just gave your answer.
registration stage
get user's password at registration - you should do this securely
using SSL.
hash and store in database = sha1(users_plai ntext_password)
login stage
1. create a random string and store in session on server,
2. send login form with username, password, and random string
3. when user enters password, set password field to
sha1( sha1(users_plai ntext_password) +random string ), and post form
auth stage
server computes sha1( users_hashed_pa ssword_in_datab ase +
$_SESSION['random_string'] )
if $_POST['password'] ==
sha1( users_hashed_pa ssword_in_datab ase + $_SESSION['random_string'] )
then OK, else not.
Sorry Erwin.

Hey! Don't appologize!
Your explanation was a lot better and clearer than my short vague response.
:-)
I didn't even mention a random string. ;-)

Regards,
Erwin

I should add, that I was assuming that "The password
field is encrypted with" meant that he initially used javascript to
hash the password client side, however on rereading - it doesnt appear
to be the case, so in this case job done, he should just run your code!
cool, and of course I forgot to mention that the point of this random
string is to be used as a one-time-pad after the login attempt is
should be expired immediately from the session, and a new form with
new random_string sent and stored in session. this prevents passive
man-in-the-middle attempts to login after you.
You could remove the random_string from the session only after
successful login, but then that would allow the passive guy to grab
your mispelt password, adjust it and try again, (s/he is probably on
your LAN and could know enough to guess the typo) so sending a new one-
time-pad with each login form is best.

It works of course because the server has to compare
if $_POST['password']
with
sha1( users_hashed_pa ssword_in_datab ase +
$_SESSION['random_string'] )
so if the random string has gone each time, then the server cannot
compute the same value for
users_hashed_pa ssword_in_datab ase + $_SESSION['random_string']
as was sent by user

Remember though, this method just protects the login, if you then use
the presense of the session ID as an "authentica tion token" anyone
could grab that and replay it, piggybacking on your session.
Piggybacking testing can be fun though, I run a free wireless node
which is unencrypted, and friends and neighbours are happy to know we
can all piggyback if we can sniff the network and adjust the cookies -
it is simply AMAZING how many sites trust the presense of a particular
session id to proove that the user has authenticated and should have
access. It is hard to do prevent this without SSL though, so perhaps
it's just prohibitive dev/cert cost, which is where:
http://www.cacert.org/
comes in!

Mar 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2060
by: circuit_breaker | last post by:
Hi, My MySQL database is all fucked up because... Never mind. I just want to reinstall it, I don't care about the data and the rest. I tried to uninistall it from Add/Remove applications but it still seems to be there (I can login to the database). Next I tried using rpm -e ..... same thing. When I try to install it again, I'm...
1
1489
by: zia | last post by:
Dear friends! I have decided to do my final Project in PHP and MySQL.Can Anybody plz tell me wether these Tools Will Support Distributed Databases. Aspecially MySQL. As some people says MySQL does not support distributed databases. Plz Reply me as soon as Possible.I would be Very thankfull for that. Regards!
2
1671
by: Kevin Cloutier | last post by:
Hi, I'm am brand new to PHP and I'm getting up to speed (slowly) running through some example tutorials. But I am having trouble getting a For Loop to work. It is supposed to return the microscond from the server each time it loops, but it is returning the same second each time through. I'm assuming this is because "function make_seed()"...
2
2994
by: PHP_Paul | last post by:
Ok, I'm trying to poineer into the wonderful area of PHP/MySQL programming, but I'm having some difficulties. http://www.paulhq.com/php/freepage.html should register, but when anyone fills something out, it returns a MySQL error: Could not insert data because You have an error in your SQL syntax; check the manual that corresponds to your MySQL...
10
351
by: Bob Bedford | last post by:
Hi all, I've ever the same problem. Table1 idperson, name, zip table2 zip, city, region.
1
1484
by: amcnpr33 | last post by:
Please forward this or reply to beth@laurus-group.com MySQL DBA/Architect - An innovator in the Internet Search Marketing industry is looking for talented, fun team members. MySQL DBA/Architect should have strong MySQL database design and optimization Open Source and Data warehouse/architect experience. This post is in R&D. Do you know...
12
2876
Rahulnewdelhi
by: Rahulnewdelhi | last post by:
hello all Hello all I installed php, mysql and IIS in xp. Problem is this while i run following file PHP Code: ---------------- <?php
0
1162
by: boyindie86 | last post by:
Hi Forum, I am having some really iritating problems with an simple input page which should pass parameters into a stored procedure from the text boxes first and last name. I keep getting the error during exection of sp_myinsert, pfirstname not defined, every time i try to submit the form Has anyone got any suggestions I have giving my...
7
1548
by: ronaldh | last post by:
can anyone help me to set up a formmail.cig i am in over my head last to days trying and no luck i have managed to completly delette my cgi doc all together and need to start from scratch please help me
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.