当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > sha1_file() 函数
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。
sha1_file(file,raw) |
Parameter参数 | Description描述 |
---|---|
file | Required. The file to be calculated 必要参数。指定需要参与计算的字符串 |
raw | Optional. Specifies hex or binary output format: 可选参数。指定输出结果的格式(包括十六进制和二进制)
Note: This parameter was added in PHP 5.0 |
<?php $filename = "test.txt"; $sha1file = sha1_file($filename); echo $sha1file; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d |
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. |