views:

330

answers:

8

Hello everyone, I want to build a simple single user login "library" in PHP, but I'm facing the following dilemma: how should I store username and password if I'm not using a database?

A simple plain text file could be easily read and then the password could be easily decripted so that's not an option.

If I create a php file with just

<?php
    $Username= "username";
    $Password= "password";
?>

then no one should be able to read it and I could simply include this file where I need it, but I'm not really sure you can't find a better way to do this!

So what's, in your opinion, the best solution to this problem (and why)?

Thanks

+3  A: 

You can store it in a file and use a SHA1/SHA2 hash, that way it can't be decrypted.

user:<sha1hash>
user:<sha1hash>
...
webdestroya
SHA2 (or bcrypt), **please**!
SLaks
+3  A: 

A plain text file is an option, and it's the simplest solution here. Simply hash the password (with salt). It is not reliably decryptable.

You can use PHP's md5 or sha1 hash functions for this.

Daniel DiPaolo
There are some tools that try to decrypt md5 and sha1, and at least in some cases they seem to work (I tried them once). I don't think the salt would be useful then, you just need to make a few tries.
Mokuchan
If you want to use a strong encryption/hash then that's fine but a flat file with a hashed password in it is definitely the easiest solution.
Daniel DiPaolo
to quote SLaks: "use SHA2 (or bcrypt), **please!** "
Jacco
**Anything** can be brute-forced. MD5 and SHA1 don't exactly have a key weakness that these tools abuse (though they *do* have key weaknesses!) It's just that they're quick algorithms, so it's not a big deal to try millions of possible hashes to find the right one. Slower algorithms are actually stronger in that sense.
Matchu
A: 
  • You can use a plain text file with a hash or crypt function. It's reliable but not really flexible if you have a lot of users.

  • You can also use SQLite which is a database but which isn't a server and which is stored in a simple file. It's a nice compromise if you can't install a SQL server but want to store a lot users and have more flexibility.

  • Depending on what you want to do, an .htaccess could be a good solution. It's already secure but as the plain text solution it's not rely flexible. But it's built-in almost all Apache configuration.

Boris Guéry
+1  A: 

If you need multiple accounts, a plain text file would be easier than using a PHP source file. I think you're worried about people requesting the file through their browser though?

Check if your web host has some kind of directory that isn't publically accessible, for example a parent directory of where the actual web content is. PHP will usually be able to read and write there, but it won't be accessible through the web.

File permissions and/or .htaccess files (Apache) may help you if there is no such directory you have access to.

I suggest you use the crypt function to store the password (hash), rather than storing plain passwords. This is a separate issue from where you are storing them though :)

Thorarin
I thought about file permissions but I wouldn't know how to set them to make it work: is php considered as "user" and external requests as "other"?
Mokuchan
@Mokuchan: Depends on the server configuration. Easiest to find out by checking `phpinfo()` output for user/group.
Thorarin
+1  A: 

What is the purpose of having a username if there's only ever one user?

Might as well just manage permissions using .htaccess...

BlueRaja - Danny Pflughoeft
It's for allowing acces to admin functions so even thought there is only one admin you still need the password at least. Maybe you don't need the username but the problem won't change.
Mokuchan
@Mokuchan: The problem won't exist if only your computer has access to the page...
BlueRaja - Danny Pflughoeft
@BlueRaja: You're right but it's for a website.
Mokuchan
@Mokuchan: You use .htaccess so that only your computer can access the administrator page. See [here](http://www.freewebmasterhelp.com/tutorials/htaccess/), [here](http://www.javascriptkit.com/howto/htaccess.shtml), and most importantly, [here](http://www.javascriptkit.com/howto/htaccess5.shtml).
BlueRaja - Danny Pflughoeft
I'd just build the admin panel as a local website existing on your own computer, and have it connect directly to the database. That is, unless you need to share some libraries. But you should be developing your website locally, anyway, so include them from your local copy :)
Matchu
+1  A: 

(This started out as a comment to Daniel DiPaolo, in response to Mokuchan.)

If you want to store a password (no matter the location), you use the following scheme:

$hashedPassword = $salt . hash( $salt . $password);

The storage location of the hashed password should be safe. Be it in a database or in a file with the proper permissions set.

If a file, your 'record' for the user bob with password secret would look something like this (using BCrypt Hash):

bob:$2a$05$tlk4M8WSpVkO7ER6QGxcwuY91MrBCQn.TCDZ5eOM1iz2sCChtR62K

There is no way for anyone to 'decrypt' the password. That's the whole point of using a Hashing algorithm: it is non-reversible.

You state that:

There are some tools that try to decrypt md5 and sha1, and at least in some cases they seem to work

As hashing algorithms are non-reversible, this is not possible. (There is no 'decrypt' option)

My best guess is that you are referring to a tool that looked up a hash from a precomputed table and it returned a valid input string, likely to be your password.
These tables are called rainbow tables. They can be defeated by A) using a random salt and B) using a strong hashing algorithm (BCrypt hash or SHA2-family hash for example)

Regarding improper hashing algorithms: MD5 and SHA1 are considered cryptographically broken. In other words: you should not be using them any more.

For a discussion on this, see: http://stackoverflow.com/questions/2768248/is-md5-really-that-bad

Jacco
A: 

Ditto to all those saying you can encrypt the password.

Also, don't put the file in the document tree. Put the file somewhere else. Your PHP program should still be able to read it by specifying an absolute path or a relative path that goes ".." however many levels to work up the hierarchy and then to where the file is. (In Java apps, there's a WEB-INF directory that is convenient for storing this sort of stuff. I don't think there's anything like that in PHP -- it's been a while since I've done any PHPing -- but you can always just put the file completely outside your application's directory hierarchy.)

Jay
A: 

Do not reinvent the wheel, use this one http://pear.php.net/manual/en/package.fileformats.file-passwd.file-passwd-unix.php

crrodriguez
thank you, but I think that reinventing the wheel is quite instructive: you can come up with interesting problems like this one and actually learn a lot.
Mokuchan