Home  >  Article  >  Backend Development  >  How to encrypt password in php? Introduction to MD5() function encryption

How to encrypt password in php? Introduction to MD5() function encryption

青灯夜游
青灯夜游Original
2018-10-08 09:29:536794browse

How to encrypt password in php? I believe that many friends who have just come into contact with php will have such questions. This chapter will introduce to you how PHP encrypts passwords and introduces the MD5() function encryption. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

md5() function introduction

md5() function calculates the MD5 hash of a string, using RSA data security, including the MD5 message digest algorithm.

The MD5 algorithm is primarily designed for digital signature applications; in this digital signature application, larger files will be encrypted (where the encryption process is performed under a cryptographic system [such as: RSA ] is done by setting the private key under the public key) before compressing in a secure manner.

md5() function syntax

md5(string,raw);

string: Specifies the string to be calculated and the required parameters.

raw: Specifies hexadecimal or binary output format, you can choose to set the parameters, there are the following two output formats:
TRUE - raw 16-character binary format;
FALSE - 32 character hexadecimal number, this is the default value.

md5() encryption is an irreversible encryption method. Let's look at an example:

$password = '123456';
echo md5($password);

This will encrypt the password "123456" into "e10adc3949ba59abbe56e057f20f883e", which is not good was cracked.

md5() function example

Let’s take a look at an example of how PHP uses md5() encryption to verify information

<!DOCTYPE html>
<html>
<body><?php
$str = "Hello";
echo md5($str);//输出加密后的密码

if (md5($str) == "8b1a9953c4611296a827abf8c47804d7")//加密后的密码和已有的密码进行比对,相同就输出Hello world!
  {
  echo "<br>Hello world!";
  exit;
  }
?>
  </body>
</html>

Running results:

How to encrypt password in php? Introduction to MD5() function encryption

We can use it together with the database to call the user name and password in the database for comparison and verification to check the information for user login and other operations.

The above is the detailed content of How to encrypt password in php? Introduction to MD5() function encryption. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn