Possible Duplicate:
Secure hash and salt for PHP passwords
For the encrypting of the password in the php file, I want to change to sha256 or md5 instead of using sha1 as iIwent to research online and they say sha1 is not so secure.
How do I change in the php file?
<?php
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $nric, $email, $license, $address, $postal_code, $password) {
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(name, nric, email, license, address, postal_code, encrypted_password, salt, created_at) VALUES('$name', '$nric', '$email', '$license', '$address', '$postal_code', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
/**
* Get user by nric and password
*/
public function getUserByNricAndPassword($nric, $password) {
$result = mysql_query("SELECT * FROM users WHERE nric = '$nric'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($nric) {
$result = mysql_query("SELECT nric from users WHERE nric = '$nric'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand()); //algorithm hash
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
you can use
instead of
in function
BCrypt is the way to go when it comes to encrypting your passwords in PHP. Here is some code that should help you along the way:
Do not spend to much time playing around with different hash algorithms and your own perfect way of storing them. Chances are that you make some mistake here, and the best hashing algorithm cannot save you.
I strongly advise people to stick to the standard libraries. They have pretty good functionality that is used by the host system anyway. This means in particular the
crypt
function, which is likely used heavily by various parts of your operating system.Now some people will get a heart attack when I mention
crypt
. And this is good, because it means they are just repeating information from the original UNIX days again, and have not understood much. Moderncrypt
can do much more thanDES
. Just don't use it withDES
.Here is part of the
crypt
man page on my Linux system (but this is also supported by BSD). All of this should be directly available in PHP, too:The massive benefit you get from using this scheme is that you can in fact have different schemes in effect in your system.
Say you have some users who set their password in a time where you were still using
MD5
. However, any new password should be encrypted usingSHA-256
. And maybe in a few years, you want to slowly migrate to yet another standard, maybeBcrypt
. No problem. It just needs a new ID. And these hashes are probably supported by all the standard software you have. Need them as unix logins? No problem. Apache HTTP authentication? no problem. Because it's using the operating system standard.And on verification, it will be tested against the scheme that was in use when the password was last set. So it is backwards compatible, and forwards compatible.
If you want to migrate to a new scheme, say
SHA-3
when it is out, you can just change the default hash to the latest, then ask the users to set a new password, and at some point disable all passwords that have the old hash ID. It makes tons of sense to store an ID along with the hash.All you need to do to use SHA-256 scheme, is to generate a Hash that has the scheme
$5$<16-chars-of-salt>$
. If you want SHA-512, use$6$<16-chars-of-salt>$
.It's very simple code:
will produce
A properly salted SHA-512 password hash. Don't reinvent the wheel.
You can change hash function to sha256, md5, or anything, but it will not work well if your DB is already filled with sha1-hashed passwords. In this case there's no way to change hash function without discarding legacy datas.
The most secure way of hashing passwords, would be to use BCrpyt
MD5, SHA1, SHA256 is considered not secure.
For more information on this matter, see this post on security: https://security.stackexchange.com/questions/211/how-to-securely-hash-passwords
For implementing BCrpyt password hashing see: How do you use bcrypt for hashing passwords in PHP?