当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > md5() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 md5() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 483 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The md5() function calculates the MD5 hash of a string.
md5()函数的作用是:计算字符串的MD5 hash。

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
md5()函数使用美国实验室(以研究加密算法而著名)数据安全加密。它采用MD5信息散列[Message-Digest]运算法则

From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA."
RFC1321的解释 - MD5信息散列[Message-Digest]运算法则:“MD5信息散列运算法则将任意长度的信息作为输入值,并将其换算成一个128位长度的“指纹信息”或“信息散列”值来代表这个输入值,并以换算后的值作为结果。MD5运算法则主要是为“数字签名程序”而设计的;在这个“数字签名程序“中,较大的文件将在加密(这里的加密过程是通过在一个密码系统下[如:RSA]的公开密匙下设置私要密匙而完成的)之前以一种安全的方式进行压缩。”

This function returns the calculated MD5 hash on success, or FALSE on failure.
如果函数执行成功将计算MD5 hash,如果失败返回false。

Syntax
语法

md5(string,raw)

Parameter参数 Description描述
string Required. The string to be calculated
必要参数。指定需要计算的字符串
raw Optional. Specifies hex or binary output format:
可选参数。指定输出结果的格式(包括十六进制和二进制)
  • TRUE - Raw 16 character binary format
    TRUE – 最原始的由16个字符表示的二进制格式
  • FALSE - Default. 32 character hex number
    FALSE – 默认值。由32位字符表示的十六进制数

Note: This parameter was added in PHP 5.0
注意:这个参数仅在PHP5.0以上版本中支持

 

Example 1
案例1

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

The output of the code above will be:
上述代码将输出下面的结果:

8b1a9953c4611296a827abf8c47804d7


Example 2
案例2

In this example we will print the result of md5() and then test it:
在这个案例当中,我们将输出md5()的结果,并对其进行测试:

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

The output of the code above will be:
上述代码将输出下面的结果:

8b1a9953c4611296a827abf8c47804d7
Hello world!

评论 (0) All

登陆 | 还没注册?