例
計算文本文件的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字符串參考