PHP Conference Japan 2024

hash_update_stream

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

hash_update_stream將資料從已開啟的串流引入作用中的雜湊內容

說明

hash_update_stream(HashContext $context, 資源 $stream, int $length = -1): int

參數

context

hash_init() 返回的雜湊內容。

stream

由任何串流建立函式返回的已開啟檔案控制代碼。

length

stream 複製到雜湊內容的最大字元數。

傳回值

stream 實際新增至雜湊演算法的位元組數。

更新日誌

版本 說明
7.2.0 接受 雜湊演算法內容 取代資源。

範例

範例 #1 hash_update_stream() 範例

<?php
$fp
= tmpfile();
fwrite($fp, 'jumped over the lazy dog.');
rewind($fp);

$ctx = hash_init('sha256');
hash_update($ctx, 'The quick brown fox ');
hash_update_stream($ctx, $fp);
echo
hash_final($ctx);
?>

上述範例將輸出

68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483

參見

  • hash_init() - 初始化一個增量雜湊演算法內容
  • hash_update() - 將資料輸入到一個作用中的雜湊演算法內容
  • hash_update_stream()
  • hash_final() - 完成增量雜湊並返回結果摘要

新增註解

使用者貢獻的註解 1 則註解

2
qiuty at mail dot ru
3 年前
這可能很明顯,但 hash_update_stream() 會移動檔案指標。因此,如果您打算在雜湊後讀取檔案,請使用 rewind()。
To Top