CakeFest 2024: The Official CakePHP Conference

hash_update_stream

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

hash_update_stream アクティブなハッシュコンテキストに、オープンしているストリームから データを投入する

説明

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

パラメータ

context

hash_init() が返すハッシュコンテキスト。

stream

ストリーム作成用の関数が返す、オープンしているファイルハンドル。

length

stream からハッシュコンテキストにコピーする 最大文字数。

戻り値

stream からハッシュコンテキストに追加された 実際のバイト数を返します。

変更履歴

バージョン 説明
7.2.0 リソースの代わりに HashContext を受け入れるようになりました。

例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_final() - 段階的なハッシュ処理を終了し、出来上がったダイジェストを返す
  • hash() - ハッシュ値 (メッセージダイジェスト) を生成する
  • hash_file() - ファイルの内容から、ハッシュ値を生成する

add a note

User Contributed Notes 1 note

up
2
qiuty at mail dot ru
2 years ago
This can be obvious, but hash_update_stream() move file pointer. So, use rewind(), if you plan to read the file after hashing.
To Top