Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

hash()

Compute the digest of the exact input bytes using the selected algorithm.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

hash(string $algo, string $data, bool $binary = false, array $options = []): string

Compute the digest of the exact input bytes using the selected algorithm. By default PHP returns lowercase hexadecimal. With binary mode SHA-256 produces 32 raw bytes instead of 64 hex characters. An unsupported algorithm raises ValueError in PHP 8.

Signature

hash(string $algo, string $data, bool $binary = false, array $options = []): string

Example output

["ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",32,true]

Examples

<?php
$hex=hash("sha256","abc"); $raw=hash("sha256","abc",true);
echo json_encode([$hex,strlen($raw),bin2hex($raw)===$hex]);

Result

["ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",32,true]

Sources