Home  >  Article  >  Backend Development  >  php calculate MD5 hash of string

php calculate MD5 hash of string

黄舟
黄舟Original
2017-11-02 13:22:291521browse

Example

Calculate the MD5 hash of the string "Hello":

<?php
$str = "Hello";
echo md5($str);
?>

Definition and usage

md5() function calculates the MD5 hash of the string.

The md5() function uses RSA data security, including the MD5 message digest algorithm.

Explanation from RFC 1321 - MD5 message digest algorithm: The MD5 message digest algorithm takes information of any length as an input value and converts it into a 128-bit length "fingerprint information" or "message" Summary" value to represent this input value, and the converted value as the result. The MD5 algorithm is primarily designed for digital signature applications where larger files are encrypted using a public key in a cryptographic system such as RSA. (done by setting a private key) before compressing in a secure manner.

To calculate the MD5 hash of a file, use the md5_file() function.

Syntax

md5(string,raw)

Parameters Description

##string Required. Specifies the string to be calculated. ​

raw ​ ​ Optional. Specifies hexadecimal or binary output format:

              TRUE - raw 16-character binary format

                                                                                                                use   use using using using ’         use using ’ s ’ s ’ s ‐   ‐ ‐ ‐ to be output in hexadecimal format. 32 characters Sixteen -in -made numbers

Technical Details

This Return Value: If successful, return the calculated MD5 scale, if it fails, it will Return FALSE.

PHP Version: 4+

##Change Log: In PHP 5.0, raw Parameters become optional.

More examples

Example 1

Output the result of md5():

<?php 
$str = "Hello"; 
echo "The string: ".$str."<br>"; 
echo "TRUE - Raw 16 character binary format: ".md5($str, TRUE)."<br>"; 
echo "FALSE - 32 character hex number: ".md5($str)."<br>"; 
?>

Example 2

Output the result of md5() and test it:

<?php
$str = "Hello";
echo md5($str);

if (md5($str) == "8b1a9953c4611296a827abf8c47804d7")
{
echo "<br>Hello world!";
exit;
}
?>

 一般来说,一个提供会员注册的网站必定需要采集用户的密码,如何保存用户密码就是个问题。我们当然不能将密码以明文的方式储存在数据库中,因为这样的话,一般的管理员就可以查看到用户密码,显然对用户来说是件相当危险的事情。 

    那如何解决这个问题呢,我们可以采取这样的策略。 

    首先介绍一下PHP中MD5函数的使用方法: 

<?php  
$pswd1=md5("cenusdesign");  
echo $pswd1;     //运行结果为:fc60ec37d1c08d5b0fb67a8cd934d5ba  

$pswd2=md5("Cenusdesign");  
echo $pswd2;     //运行结果为:067577d9fc109c80538c81d6f02bd293  
?>


    显然,经过md5加密后,原本“cenusdesign”转变成了一组32位的字符串,而且,即使是一个字母的大小写变化,这组字符串就会发生巨大的变化。 

    Cenus Design建议把用户注册时,将密码首先经过MD5转换,然后将转换加密后的数据库。在用户登陆时,也将密码先进行MD5转化,再和数据库中那组经过MD5加密的字符串进行比较。这样,就可以做到在不知道用户确切密码的情况下完成密码的比对操作。 

The above is the detailed content of php calculate MD5 hash of string. 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