473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 5586
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_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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, "sathyashrayan" <asm_f...@yahoo.co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller

<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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_plaintext_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_plaintext_password)+random string ), and post form
auth stage
server computes sha1( users_hashed_password_in_database +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_password_in_database + $_SESSION['random_string'] )

then OK, else not.

Mar 26 '07 #4
On 26 Mar, 15:35, "shimmyshack" <matt.fa...@gmail.comwrote:
On 26 Mar, 13:29, "sathyashrayan" <asm_f...@yahoo.co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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_plaintext_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_plaintext_password)+random string ), and post form

auth stage
server computes sha1( users_hashed_password_in_database +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_password_in_database + $_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, "sathyashrayan" <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*********@gmail.com
"Olemme apinoiden planeetalla."
Mar 26 '07 #7
shimmyshack wrote:
On 26 Mar, 15:35, "shimmyshack" <matt.fa...@gmail.comwrote:
>On 26 Mar, 13:29, "sathyashrayan" <asm_f...@yahoo.co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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_plaintext_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_plaintext_password)+random string ), and post form

auth stage
server computes sha1( users_hashed_password_in_database +
$_SESSION['random_string'] )

if $_POST['password'] ==
sha1( users_hashed_password_in_database + $_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_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
shimmyshack wrote:
On 26 Mar, 15:35, "shimmyshack" <matt.fa...@gmail.comwrote:
On 26 Mar, 13:29, "sathyashrayan" <asm_f...@yahoo.co.ukwrote:
On Mar 26, 4:59 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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_plaintext_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_plaintext_password)+random string ), and post form
auth stage
server computes sha1( users_hashed_password_in_database +
$_SESSION['random_string'] )
if $_POST['password'] ==
sha1( users_hashed_password_in_database + $_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_password_in_database +
$_SESSION['random_string'] )
so if the random string has gone each time, then the server cannot
compute the same value for
users_hashed_password_in_database + $_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 "authentication 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
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...
1
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...
2
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...
2
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...
10
by: Bob Bedford | last post by:
Hi all, I've ever the same problem. Table1 idperson, name, zip table2 zip, city, region.
1
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...
12
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
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.