How to Encrypt Passwords in PHP?

Last updated on September 8, 2023

In PHP, there are several functions and methods that can be used to securely hash and encrypt passwords. Below is the list,

  • md5()
  • password_hash()

md5() Function:

In PHP, md5 function is commonly used password encryption method which stands for MD5 Message-Digest Algorithm. It is not recommended for secure passwords. It is a one-way hashing algorithm and supported by PHP versions PHP 4, 5, 7, and 8.

Parameters:

The md5() function takes two parameters the first is required and the second is optional. The default is FALSE.

Syntax:

md5(string,raw);

Return values:

  • By default, it returns a 32-character hexadecimal number.
  • If the optional binary parameter is set to true, it returns the raw binary format of the MD5 hash, which is a 16-byte (not 16-character) string.

Look at the below example in which we passed string into md5() function to make the 32-character hexadecimal hash.

$string = "YouPasswordHere"; 
echo md5($string);  
// output : f91adb38d9f43c865c1254a975eaa210. 

In the above example, we print the hash value generated by the md5() function.

Look at another example in which we set optional binary parameter to true and passed the string.

$string = "YouPasswordHere";
echo md5($string, true);

// output : ��8��<�\T�u�

In this example, we passed the string to md5() function and set second argument to true which returns 16-byte string.

Conclusion :

This article explains PHP built-in functions that is used to encrypt the passwords along with examples.


Written by
I am a web developer with over 2 years of industry experience, specializing in PHP, JavaScript, MySQLi, and Laravel. I create dynamic and user-friendly web applications, committed to delivering high-quality digital solutions.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP, PHP-Functions,