*/ class hash { protected $raw; protected $hmac; protected $key; protected $algo; /** * constructor to validate algorithm * * @param string $algo * @param optional $raw * @param optional $hmac * @param optional $key */ public function __construct ($algo, $raw = Null, $hmac = Null, $key = Null) { $this->raw = $raw; $this->hmac = $hmac; $this->key = $key; if (in_array($algo, hash_algos())) { $this->algo = $algo; } else { die("algorithm ".$algo." not supported"); } } /** * hash given data with given algorithm * * @param mixed $data * @return hash */ public function hash ($data) { if (!empty($this->hmac)) { return hash_hmac($this->algo, $data, $this->key, $this->raw); } else { return hash($this->algo, $data, $this->raw); } } /** * hash given file with given algorithm * * @param mixed $file * @return hash */ public function hash_file ($file) { if (!empty($this->hmac)) { return hash_hmac_file($this->algo, $file, $this->key, $this->raw); } else { return hash_file($this->algo, $file, $this->raw); } } /** * compare the given data * * @param hash $hash * @param mixed $data * @return boolean */ public function compare ($hash, $data) { $newhash = $this->hash($data); if ($newhash == $hash) { return true; } return false; } /** * compare the given file * * @param hash $hash * @param mixed $file * @return boolean */ public function compare_file ($hash, $file) { $newhash = $this->hash_file($file); if ($newhash == $hash) { return true; } return false; } } ?>