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

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 中的 sha1_file() 函数


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

Definition and Usage
定义和用法

The sha1_file() function calculates the SHA-1 hash of a file.
sha1_file()函数的作用是:计算文件的SHA-1 hash。

The sha1_file() function uses the US Secure Hash Algorithm 1.
sha1()函数使用的是美国 Secure Hash 运算法则1。

From RFC 3174 - The US Secure Hash Algorithm 1: "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."
RFC1321的解释 -美国 Secure Hash 运算法则1:“SHA-1创造出了一个名为信息散列的160位结果。举个例子来说,这个信息散列可以输进一个用来产生和验证信息签名的签名档运算法则中。给信息散列进行署名并不意味着对信息进行改进,或是增强信息的处理效率。这是因为:信息散列在通常状况下都会比原信息来的小。相同的hash运算法则必须通过一个数字签名验证器(这个数字签名验证器是给数据签名的创建者使用的)来使用,”

This function returns the calculated SHA-1 hash on success, or FALSE on failure.
如果这个函数成功执行,将返回计算之后的SHA-1 hash,如果执行失败,将返回False。

Syntax
语法

sha1_file(file,raw)

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

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



Example 1
案例1

<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?> 

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

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d


Example 2
案例2

Store the SHA-1 hash of "test.txt" in a file:
在下面的案例中,我们将输出sha1()函数的结果并对其进行测试:

<?php
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>

In this example we will test if "test.txt" has been changed (that is if the SHA-1 hash has been changed):
在下面的案例中,我们将测试是否“test.txt”被改变了(即:测试是否SHA-1 hash被改变了):

<?php
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("test.txt") == $sha1file) { echo "The file is ok."; }
else { echo "The file has been changed."; }
?> 

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

The file is ok.

评论 (0) All

登陆 | 还没注册?