3v4l.org

run code in 300+ PHP versions simultaneously
<?php // i wish the php builtin HashContext class had this stuff: class OOHashContext{ private $hc; public const HASH_HMAC=1; public function __construct(string $algo, int $options = 0, string $key = NULL){ if(!($this->hc = hash_init($algo, $options, $key))){ throw new \RuntimeException(); } } public function update(string $data):self{ if(!hash_update($this->hc, $data)){ throw new \RuntimeException(); } return $this; } public function update_file(string $file, $scontext = null):self{ if(!hash_update_file($this->hc, $file, $scontext)){ throw new \RuntimeException(); } return $this; } public function update_stream($handle, int $length = -1):self{ if(!hash_update_stream($this->hc, $handle, $length)){ throw new \RuntimeException(); } return $this; } public function final(bool $raw_output = false):string{ $ret = hash_final($this->hc, $raw_output); if(!is_string($ret)){ throw new \RuntimeException(); } return $ret; } } $o=new OOHashContext("SHA1"); $str1="foo"; $str2="bar"; $result = (new OOHashContext("SHA1"))->update($str1)->update($str2)->final(); echo $result;

preferences:
25.1 ms | 408 KiB | 5 Q