<?php /** * TSHash v1.0 * @copyright tutorialspots.com * Simplest and fastest Hash method **/ class TSHash{ public $seed; public $result; private $chunks = array(); private $end = ""; function __construct($seed = 0x11afec09){ $this->result = $this->seed = $seed; } function update($str){ if($str=='') return $this; $str = $this->end.$str; $strs = str_split($str,4); $end = array_pop($strs); $this->chunks = array_merge($this->chunks,$strs); if(strlen($end)==4){ $this->chunks[] = $end; $this->end = ''; }else{ $this->end = $end; } foreach($this->chunks as $s){ $vv= unpack('l*',$s)[1]; $this->result = $this->result ^ $vv; $this->result = $this->result ^ ($vv>>2); } return $this; } function digest(){ if(strlen($this->end)>0){ $vv = 0; for($i=strlen($this->end)-1;$i>=0;$i--){ $vv += pow(256, strlen($this->end)-1-$i) * unpack('c*',$this->end[$i])[1]; } $this->result = $this->result ^ $vv; $this->result = $this->result ^ ($vv>>2); } //$this->result = $this->result & 0xffffffff; return $this; } function toHex(){ $result = pack('l*',$this->result); $ret = ""; for($i=strlen($result)-1;$i>=0;$i--){ $ret .= unpack('H*',$result[$i])[1]; } return $ret; } }
Example:
$Hash = new TSHash(); echo $Hash->update('bcdef')->update('ghijklm')->digest()->toHex();
Result:
14aae306