How to generate password salt with codeigniter - php

Sorry for being new in php programming, in my old project I use MD5 to encrypt the password, however, it is not secure enough and I found some resource on the internet suggest using password salt instead.
The problem is , I am using codeigniter, is there any helper/ library for this purpose / how to change my old code to support the generation of the password salt?
Thanks for helping. I am using PHP 5.2
And here is the old code to validate, while the user account generate by storing the md5($password);
function validate_credentials() {
$this->load->model('Secure_model');
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$is_valid = $this->Secure_model->validate('customer', $username, $password);
if ($is_valid) {
$data = array(
'user_id' => $this->get_user_id($username),
'user_name' => $username,
'is_logged_in_user' => true
);
$this->session->set_userdata($data);
redirect('profile');
} else {
$data['message_error'] = TRUE;
$data['main_content'] = 'front/login';
$this->load->view('front/includes/template', $data);
}
}

If you are really stuck with PHP 5.2 your best bet will propably be the phpass library, because there is no PHP support of the BCrypt algorithm.
PHP versions 5.3 and later will have native support of BCrypt, so you can use the PHP function password_hash() to hash a password. There is a compatibility pack for versions before 5.5.
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
In every case you are doing right with discarding MD5 and switching to another algorithm. Make sure that you use an algorithm with a cost factor like BCrypt or PBKDF2, fast algorithms like SHA* are not appropriate to hash passwords. Salting is mandatory, though the salt can be stored in the database, it fulfills its purpose even if it is known.

look this part of my code I use to register an user:
public function addUser($data){
$sql = "INSERT INTO `user` salt=" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) .", password=".$this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))).".......";
$this_>db->query($sql);
The information of salt and password are stored in your user table.
To retrieve the information and validate the password you do this:
$query = $this->CI->db->query("SELECT * FROM `user` WHERE email =".$this->CI->db->escape($email)." AND password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(" . $this->CI->db->escape($password) . ")))))");

Here are some simple solutions.
You can use sha* hash functions , be careful in using md5 since it has a
higher rate of collisions than sha,
and also about your problem with salt, it is ok if you dont salt your
password, just make sure your users use a very good password with a combination of
lower and upper cases and with numbers and make them lengthy.
I would like to advise you to use bcrypt but since you are using 5.2 it has a bug on that version and certain password libs like PHPPASS and PHPLIB Cater Only to 5.3 and above. Best option is to upgrade to 5.3 so that you can use the php libs, but take care full caution the scripts.

As far as I know codeigniter does not have a built-in function for this...
To make a hash with PHP you need
the password
a true random salt
a slow hashing algorithm
By PHP your can create a true random salt by using mcrypt_create_iv().
To make the hash, you can use the crypt() or password_hash, which supports slow algorithms, like CRYPT_BLOWFISH. Forget md5, or sha1, they are too fast, so with the proper tool it is possible to find out passwords hashed by them.
$salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
$hash = password_hash($password, PASSWORD_BCRYPT, array('cost' => 11, 'salt' => $salt));
The password_hash() function can generate a true random salt automatically, so you don't have to generate it manually if you don't want. The salt will be appended to the hash.
Sadly PHP 5.2 does not have CRYPT_BLOWFISH support. So you have to use the PHPASS lib.

You should set a $config['salt] = '$%#~De#';// in your config file
//Inside your model or controller where you are getting your post values
$password = sha1($this->config->item('salt').$this->input->post->('password')));
This should give you has password

Related

Migrate from SHA256 hashed passwords to BCRYPT in PHP7+MySQL

I have an old web application with a few users registered that is using the unsecure hash("sha256", trim($_POST["password"])) to store the hashed password in MySQL database. Now I want to update the web application to use the more secure BCRYPT password_hash() however I don't want to email all registered users alerting them to change their password. So I was thinking on implementing BCRYPT on the sha256() hashed password this way:
To save the password I will sha256() hash the user's password:
$hashed_password = password_hash(hash("sha256", trim($_POST["password"])), PASSWORD_BCRYPT);
Then I will save the BCRYPT hashed password in the database.
And to verify the user's password I would simply do this:
$hashed_password = "select hashed_password from users where email = 'abc#email.com'";
if(password_verify(hash("sha256", trim($_POST["password"])), $hashed_password))
{
echo "Welcome";
}
else
{
echo "Wrong Password!";
}
This way I will just update the user's password in the MYSQL database by looping each registered user, then I will retrieve the sha256() hashed password, and finally I will just re-save it after it has been BCRYPTed with password_hash():
$new_password = password_hash($old_sha256_hashed_password, PASSWORD_BCRYPT);
$mysql->save_user_password($new_password, $user_id);
So users will still be able to login with their old password.
What do you think about this solution?
Is it still safe even if I sha256() hash the password before BCRYPT it?
Since your current hashing system (unsalted SHA256) is indeed very unsecure, you could give immediate protection to the passwords with double hashing. As soon as possible, when the user logs in the next time, I would switch to the new algorithm and remove double hashing.
Make old hashes more secure:
$doubleHashToStoreInDb = password_hash($oldUnsaltedSha256HashFromDb, PASSWORD_DEFAULT);
Doing this for each row will protect the otherwise unsecurely stored passwords. Note the PASSWORD_DEFAULT parameter, it should be prefered over a specific algorithm, because it is future proof. And mark the double hashes, so you can distinguish between double hashes and already converted hashes, see why.
Handle new user registrations:
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);
Just use the new algorithm without double hashing.
Verify logins:
if (checkIfDoubleHash($storedHash))
{
$correctPassword = password_verify(oldPasswordHash($_POST["password"]), $storedHash);
if ($correctPassword)
storeConvertedHash(password_hash($_POST['password'], PASSWORD_DEFAULT));
}
else
{
$correctPassword = password_verify($_POST['password'], $storedHash);
}
// Hashes the user password with a deprecated hashing scheme
function oldPasswordHash($password)
{
return hash("sha256", trim($password));
}
Double hashes will be converted to the new password hash function, this is possible because we have the original user password at this moment. New hashes are verified with password_verify() which is a future proof and backwards compatible function.
Adapting the password algorithm to future hardware is not a one-time task, it will be necessary as soon as new hardware will become faster. PHP offers the function password_needs_rehash() to find out whether a rehashing is necessary, then you can also calculate a new hash and store it.

Comparing hash value of password in PHP

As part of learning php, I wanted to try a register and login page, however, the site I'm following for how to store a password uses MySQLI and I'm not using that:
Hashing the password
$password1 = 'hello123';
// A higher "cost" is more secure but consumes more processing power
$cost = 10;
// Create a random salt
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
// Prefix information about the hash so PHP knows how to verify it later.
// "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
$salt = sprintf("$2a$%02d$", $cost) . $salt;
// Value:
// $2a$10$eImiTXuWVxfM37uY4JANjQ==
// Hash the password with the salt
$hash = crypt($password1, $salt);
I'm stuck on retrieving the password however, here's the site's code for it:
$username = 'Admin';
$password = 'gf45_gdf#4hg';
$sth = $dbh->prepare('
SELECT
hash
FROM users
WHERE
username = :username
LIMIT 1
');
$sth->bindParam(':username', $username);
$sth->execute();
$user = $sth->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash
if ( crypt($password, $user->hash) === $user->hash ) {
// Ok!
}
From what I can see, he grabs the hash value of the password for the user in the DB and compares the password that was passed using the hash and check with the one in the DB.
I've been trying this but the result hash is never the same as the original one:
$pwdtocheck = 'hello123';
// no call do DB yet, doing this on the same page after hashing, the $hash is the same as above
$pwdhash = crypt($pwdtocheck, $hash);
// if I echo $pwdhash it's never exactly the same as the $hash.
if ( $pwdhash === $hash) {
echo "same pwd";
}
I cannot see the actual problem in your code, maybe your database field is smaller than 60 characters, or you are comparing different passwords. In every case there is an easier and safer way to hash passwords, just use the new functions password_hash() and password_verify(). There exists also a compatibility pack for earlier PHP versions.
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
EDIT:
I see now that you are using a salt when you compare the passwords. In your line:
$pwdhash = crypt($pwdtocheck, $hash);
the $hash variable has the salt prepended to it because crypt() will automatically do that for you. crypt() will extract the salt from the $hash because it knows the expected length of the salt based on the algorithm used. See the documentation.
I'll keep my original answer below for context and for those looking for a similar answer.
END EDIT
The password is not the same for you because you are using a salt when you originally hash the password to put in your database, but you are not salting the password later when you check against the database.
You should use the same salt string when you save the password as when you check the user's password on login. Usually, you will randomly generate the salt string for each password (as you are doing) and then save the salt string to the database along with the hashed password (either in the same column or its own column) so that you can use the same salt to check the user's password on login.
See https://crackstation.net/hashing-security.htm#salt for reference.
I can't access the article you're referencing but I imagine:
You need to check using the salt as the salt and not the hash.
crypt($pwdtocheck, $user->salt) == $user->hash
should work

Reproducing PHPs hash_hmac functionality in MYSQL

I'm creating a more secure user password table with the username, a strongly random salt, and a salted/hashed password in php like so:
$salt = bin2hex(openssl_random_pseudo_bytes(64));
$pass = hash_hmac('sha512', $plainTextPass, $salt);
$this->user->password = $pass;
$this->user->salt = $salt;
$this->user->save();
I'm trying to reproduce this functionality in MySQL without much luck. I need to create some users when the DB server is deployed from a script. As I understand it the hash_hmac method just takes the salt, appends the password to it, then hashes it with sha 512. Seems like this would be easy enough to do in MySQL, but this:
INSERT INTO `users` (`email`, `username`, `password`, `salt`)
VALUES('test#example.com', 'admin', SHA2('9d1ebf3559baf9ffca61df5cec0b9eb067ae7912d097ad45f3a3086c39bf11292d92a7dfad497557fac7fbf7c24209db8b7696664d54c7e6bc55f720121bd38dadmin', 512), '9d1ebf3559baf9ffca61df5cec0b9eb067ae7912d097ad45f3a3086c39bf11292d92a7dfad497557fac7fbf7c24209db8b7696664d54c7e6bc55f720121bd38d');
In this case the salt is just a hard coded one I generated manually, and you can see I just append the password "admin" on to the end of it for the password field. This user creates, but I can't login with that admin username and password.
Should I be doing this a different way in MySQL?
An HMAC is not computed by concatenation, but as follows (see the link for more details).
HMAC (K,m) = H ((K ⊕ opad) ∥ H ((K ⊕ ipad) ∥ m))
However, as the name implies, it is a message authentication code, not a password hashing scheme. An algorithm that is designed for password hashing would be a better choice. scrypt, bcrypt, or PBKDF2 would be a good bet.

Password Hashing API Query

So I'm using the new PHP 5.5 Password Hashing API, and I'm not sure if I got this correctly.
I've tried automatically rehashing every login and sometimes I fail, even when the hashing turns out to be the same anyways, I feel like I'm doing something wrong.
It could be the query function that I probably got wrong, because the hashes don't even change when I check phpMyAdmin.
if (password_needs_rehash($result_row->user_password_hash, PASSWORD_DEFAULT))
{
$newhash = password_hash(
$_POST['user_password'], PASSWORD_BCRYPT,
['cost' => 12, 'salt' => 'superfreakingsonicdude',]
);
// update hash in database
$this->connection->query(
"UPDATE users SET user_password_hash='" . $newhash .
"' WHERE user_name='".$result_row->user_name."'"
);
}
Here is where you can find all the functions.
The funcion password_needs_rehash has been introduced to check if you need to upgrade:
password_needs_rehash($result_row->user_password_hash, PASSWORD_DEFAULT)
This function checks to see if the supplied hash implements the algorithm and options provided. If not, it is assumed that the hash needs to be rehashed.
If you have problems to understand what this function does, the RFC contains the function in PHP code. So if you can read PHP code, you should be able to read the following (see the part introduced as It could be implemented in user-land by:): https://wiki.php.net/rfc/password_hash#password_needs_rehash
Makes sense to test if the hash in the database (store) is of the same algorithm as in PASSWORD_DEFAULT or not. That means to check if PASSWORD_DEFAULT has been changed between the time the hash has been stored last time and now.
Right now PASSWORD_DEFAULT is PASSWORD_BCRYPT so it should always return false. In your case it returns true, because you're testing without your password options.
Change that and you should be fine:
$options = ['cost' => 12, 'salt' => 'superfreakingsonicdude',];
########
if (password_needs_rehash($result_row->user_password_hash, PASSWORD_DEFAULT, $options))
########
{
$newhash = password_hash($_POST['user_password'], PASSWORD_DEFAULT, $options);
################ ########
// update hash in database
$this->connection->query(
"UPDATE users SET user_password_hash='" . $newhash .
"' WHERE user_name='".$result_row->user_name."'"
);
}
Also consider to continue to use PASSWORD_DEFAULT if you want to benefit from a default hashing algo update in PHP core.
The input to the hash is the password and salt. Same password, same salt, same result.
If you leave the salt parameter out, a random salt will be generated each time and you should get a different result. You should not provide a static salt. This means all users have the same salt, which greatly diminishes its effectiveness. Each individual hash needs should have a random salt.

Salting my hashes with PHP and MySQL

Like most users, I'm simply trying to figure out a secure way to store passwords. What I haven't found here (or maybe it's my lack of understanding) is how to retrieve a salted hash in my database and separate the salt from the hashed password, especially with unique salts to each password while maintaining the salt+password in a single column.
I'm finding all these cool ways to encrypt passwords (SHA-256, but does MySQL only support SHA/1 and MD5?) and other things from the PHP manual, but not sure how store and retrieve the passwords.
So, far this is all I understand:
SHA('$salt'.'$password') // My query sends the password and salt
// (Should the $salt be a hash itself?)
After that I'm lost with salts.
Retrieving the password without a salt is easy, but the salt confuses me. Where do I get the value from $salt again, especially if it's unique and secure? Do I hide them in another database? Constant (seems unsafe)?
EDIT: Is the key variable in HMAC supposed to be salt or is this something else?
First of all, your DBMS (MySQL) does not need to have any support for cryptographic hashes. You can do all of that on the PHP side, and that's also what you should do.
If you want to store salt and hash in the same column you need to concatenate them.
// the plaintext password
$password = (string) $_GET['password'];
// you'll want better RNG in reality
// make sure number is 4 chars long
$salt = str_pad((string) rand(1, 1000), 4, '0', STR_PAD_LEFT);
// you may want to use more measures here too
// concatenate hash with salt
$user_password = sha512($password . $salt) . $salt;
Now, if you want to verify a password you do:
// the plaintext password
$password = (string) $_GET['password'];
// the hash from the db
$user_password = $row['user_password'];
// extract the salt
// just cut off the last 4 chars
$salt = substr($user_password, -4);
$hash = substr($user_password, 0, -4);
// verify
if (sha512($password . $salt) == $hash) {
echo 'match';
}
You might want to take a look at phpass, which also uses this technique. It is a PHP hashing solution which uses salting amongst some other things.
You should definitely take a look at the answer to the question WolfOdrade linked to.
Personally I recommend letting MySQL do this with its built in functions.
They way I do this is to create a function in my database config file which returns a key string. The config file should be outside your sites root so that the webserver can access the file but not others. so for example:
function enc_key(){
return "aXfDs0DgssATa023GSEpxV";
}
Then in your script use it with the sql query and AES_ENCRYPT and AES_DECRYPT functions in MySQL like this:
require_once('dbconf.inc.php');
$key = enc_key();
//When creating a new user
$sql = "INSERT INTO users (username, password) VALUES ('bob', AES_ENCRYPT('{$key}', {$password}))";
//When retrieving users password
$sql = "SELECT AES_DECRYPT('{$key}', password) AS password FROM users WHERE username like 'bob'";

Categories