*/ class hash { /** * raw output * @var string */ protected $raw = ''; /** * hash with hmac * @var string */ protected $hmac = ''; /** * hmac key * @var string */ protected $key = ''; /** * hash algorithm * @var string */ protected $algo = ''; /** * constructor to validate algorithm * * @param string $algo * @param string $raw * @param string $hmac * @param string $key */ public function __construct ($algo, $raw = '', $hmac = '', $key = '') { $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 string $data * @return string */ 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 string $file * @return string */ 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 string $hash * @param string $data * @return boolean */ public function compare ($hash, $data) { $newhash = $this->hash($data); if ($newhash == $hash) { return true; } return false; } /** * compare the given file * * @param string $hash * @param string $file * @return boolean */ public function compare_file ($hash, $file) { $newhash = $this->hash_file($file); if ($newhash == $hash) { return true; } return false; } } ?>