PHP8 hash_file

2024-03-01 09:26 更新

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)

hash_file — 給指定文件的內(nèi)容生成散列值

說(shuō)明

hash_file(
    string $algo,
    string $filename,
    bool $binary = false,
    array $options = []
): string|false

參數(shù) 

algo

要使用的散列算法的名稱(chēng)(例如:“md5”、“sha256”、“haval160,4”等)。 可以在 hash_algos() 中查看當(dāng)前支持的算法。

filename

要進(jìn)行散列運(yùn)算的文件位置的 URL;支持 fopen 封裝協(xié)議。

binary

設(shè)置為 true 時(shí),輸出原始二進(jìn)制數(shù)據(jù)。 設(shè)置為 false 時(shí),輸出小寫(xiě)的十六進(jìn)制字符串。

options

各種散列算法的一系列選項(xiàng)數(shù)組。目前 MurmurHash 算法僅支持 “seed” 參數(shù)。

返回值 

如果 binary 設(shè)置為 true, 則返回原始二進(jìn)制數(shù)據(jù)表示的信息摘要, 否則返回十六進(jìn)制小寫(xiě)字符串格式表示的信息摘要。

更新日志 

版本說(shuō)明
8.1.0新增 options 參數(shù)。

示例 

示例 #1 hash_file() 示例

<?php
/* Create a file to calculate hash of */
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');

echo hash_file('sha256', 'example.txt');
?>

以上示例會(huì)輸出:

68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483

參見(jiàn) 

  • hash() - 生成散列值(消息摘要)
  • hash_hmac_file() - 使用 HMAC 方法和給定文件的內(nèi)容生成帶密鑰的散列值
  • hash_update_file() - 從文件向活躍的散列運(yùn)算上下文中填充數(shù)據(jù)
  • md5_file() - 計(jì)算指定文件的 MD5 散列值
  • sha1_file() - 計(jì)算文件的 sha1 散列值


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)