$digest) { try { $hashAlgorithm = self::hashAlgorithmFor($algorithm); } catch (InvalidArgumentException) { continue; } if (!hash_equals(hash($hashAlgorithm, $body, true), $digest)) { return false; } $matched = true; } return $matched; } /** @return array [algorithm => raw bytes] */ public static function parse(string $header): array { $out = []; foreach (explode(',', $header) as $entry) { $entry = trim($entry); if ($entry === '') { continue; } if (!preg_match('#^([a-z0-9-]+)=:([A-Za-z0-9+/=]*):$#', $entry, $m)) { continue; } $decoded = base64_decode($m[2], true); if ($decoded === false) { continue; } $out[strtolower($m[1])] = $decoded; } return $out; } private static function hashAlgorithmFor(string $algorithm): string { return match (strtolower($algorithm)) { self::ALGO_SHA256 => 'sha256', self::ALGO_SHA512 => 'sha512', default => throw new InvalidArgumentException('unsupported content-digest algorithm: ' . $algorithm), }; } }