How to encrypt password in PHP

Theory of Computation
In this article, you will learn different ways to encrypt password using PHP. Generally, data is most vulnerable when it is being moved from one location to another. Encryption is the process through which information is encoded so it stays hidden from unauthorized users. It ensures private data, sensitive data, and can improve the security of communication between client apps and servers. Today, almost every application need password encryption technique to protect sensitive information of its users.
PHP provides a range of different encryption methods. PHP has a hash algorithm to encrypt the password. The mostly used functions for password encrypting are md5(), crypt() and password_hash().
Suppose we have the registration form data containing username and password in the POST. If we insert the same password as received in the POST in the database, this is not the secure way. If the database falls into the wrong hands, then they can misuse the data.
MD5 hashing algorithm generates a 32 characters string (hash hexadecimal number) for any word or phrase we give in the input. This is one-way encryption algorithm. The password encrypted with this algorithm can never be decrypted. The md5 is the most commonly used encryption method. The md5() function is used to calculate the md5 hash of a string. The syntax of the md5() function is -
md5(string,raw)
Here, the string is the string to be encrypted and row is an optional parameter. It specifies the output format which can either be TRUE or FALSE. The default is FALSE.
The given code encrypts the password value and stores it in the database.

 <?php
 $conn = new mysqli('hostname', 'username', 'password', 'databasename');
 $pwd = $_POST['password'];
 $encrypted_pwd = md5($pwd);
 $username = $_POST['username']; 
 $insert ="INSERT into an_users (id, username, password) 
 VALUES  ('', '$username', '$encrypted_pwd')";
 if($conn->query($insert)){
  echo 'Data inserted successfully';
 }
 else{
  echo 'Error '.$conn->error;  
 }
?>

The crypt() function returns a hashed string using salt. This method generates a weak password without salt. It takes a second parameter for the salt, which is an optional parameter. The salt is a formatted string that tells the crypt() method which algorithm is used to do the hashing. It returns a hashed string using DES, Blowfish, or MD5 algorithms. Make sure to specify a strong enough salt for better security.
crypt($string, $salt);
There are many salt constants, but here we have used CRYPT_MD5. This generates 12 characters salt.
<?php
  $conn = new mysqli('hostname', 'username', 'password', 'databasename');
  $pwd = $_POST['password'];
  if(CRYPT_MD5 == 1) {
      $encrypted_pwd = crypt($pwd, '$12$hrd$reer');
  }
  $username = $_POST['username'];
  $insert = "INSERT INTO  an_users (id, username, password) 
		  VALUES('', '$username', '$encrypted_pwd')";
  if($conn->query($insert)){
	echo 'Data inserted successfully';
  }
  else{
	echo 'Error '.$conn->error;  
  }
?>

The password_hash() method creates a new password hash using a strong one-way hashing algorithm. It means that the only way to validate a hashed output is to pass the original value to the hashing algorithm and compare the results. This makes hashing perfect for storing user passwords. The password_hash() function is compatible with crypt() function. It is implemented in PHP 5.1. The syntax of password_hash() is -
password_hash(string, algorithm, options)
Here, string is the string to be encrypted, algorithm denotes the algorithm to use when hashing the password and options is an associative array containing options.
The given code encrypts the password value using password_hash() and stores it in the database.
<?php
// Database connection
 $conn = new mysqli('hostname', 'username', 'password', 'databasename');

 $pwd = $_POST['password'];

// hash it with PASSWORD_DEFAULT
$hash = password_hash($pwd,  
          PASSWORD_DEFAULT); 
$username = $_POST['username']; 

$insert ="INSERT into an_users (id, username, password) 
 VALUES  ('', '$username', '$hash')";

if($conn->query($insert)){
  echo 'Data inserted successfully';
}
 else{
  echo 'Error '.$conn->error;  
}
?>

Jan 3
Stateful vs Stateless
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

Dec 29
Best programming language to learn in 2021
In this article, we have mentioned the analyzed results of the best programming language for 2021...

In this article, we have mentioned the analyzed results of the best programming language for 2021...

Dec 20
How is Python best for mobile app development?
Python has a set of useful Libraries and Packages that minimize the use of code...

Python has a set of useful Libraries and Packages that minimize the use of code...

July 18
Learn all about Emoji
In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

Jan 10
Data Science Recruitment of Freshers
In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...






eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.