sha1_file
計算文件的sha1哈希值
sha1_file()
函數計算文件的SHA-1 散列。
sha1_file()
函數使用美國Secure Hash 算法1。
來自RFC 3174 的解釋- 美國Secure Hash 算法1:SHA-1 產生一個名為報文摘要的160 位的輸出。報文摘要可以被輸入到一個可生成或驗證報文簽名的簽名算法。對報文摘要進行簽名,而不是對報文進行簽名,這樣可以提高進程效率,因為報文摘要的大小通常比報文要小很多。數字簽名的驗證者必須像數字簽名的創建者一樣,使用相同的散列算法。
如果成功則返回已計算的SHA-1 散列,如果失敗則返回FALSE。
計算文本文件"test.txt" 的SHA-1 散列:
<?php $filename = "test.txt" ; $sha1file = sha1_file ( $filename ) ; echo $sha1file ; ?>
以上代碼的輸出:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
在文件中存儲"test.txt" 的SHA-1 散列:
<?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.
sha1_file ( file , raw )
參數 | 描述 |
---|---|
file | 必需。規定要計算的文件。 |
raw |
可選。布爾值,規定十六進製或二進制輸出格式:
|