例
计算文本文件的SHA-1散列"test.txt" :
<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
代码的输出将是:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
定义和用法
所述sha1_file()函数计算文件的SHA-1散列。
该sha1_file()函数使用美国安全散列算法1。
从RFC 3174 -美国安全散列算法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."
该函数返回成功计算出SHA-1哈希值,或失败返回FALSE。
句法
sha1_file( file,raw )
参数 | 描述 |
---|---|
file | 需要。 要计算的文件 |
raw | 可选的。 一个布尔值,它指定十六进制或二进制输出格式:
|
技术细节
返回值: | 成功则返回计算的SHA-1散列,或FALSE的失败 |
---|---|
PHP版本: | 4.3.0+ |
更新日志: | 原始参数在PHP 5.0起成为可选项 作为PHP 5.1,它是能够使用sha1_file()与包装材料,例如sha1_file("http://w3ii.com/..") |
更多示例
实施例1
商店的SHA-1散列"test.txt"在文件中:
<?php
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>
如果测试"test.txt"已更改(即如果SHA-1散列已经改变):
<?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 file is ok.
<PHP字符串参考