* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class hash {
+ protected $raw;
+ protected $hmac;
+ protected $key;
protected $algo;
/**
* constructor to validate algorithm
*
- * @param string $algo
+ * @param string $algo
+ * @param optional $raw
+ * @param optional $hmac
+ * @param optional $key
*/
- public function __construct ($algo) {
+ 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;
+ $this->algo = $algo;
} else {
die("algorithm ".$algo." not supported");
}
* hash given data with given algorithm
*
* @param mixed $data
- * @param optional $raw
- * @return hash
- */
- public function hash ($data, $raw = NULL) {
- return hash($this-algo, $data, $raw);
- }
-
- /**
- * hash given file with given algorithm
- *
- * @param mixed $file
- * @param optional $raw
- * @return hash
- */
- public function hash_file ($file, $raw = Null) {
- return hash_file($this-algo, $data, $raw);
- }
-
- /**
- * hash given data with given algorithm
- *
- * @param mixed $data
- * @param mixed $key
- * @param optional $raw
* @return hash
*/
- public function hash_hmac ($data, $key, $raw = Null) {
- return hash_hmac($this-algo, $data, $key, $raw);
+ 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
- * @param mixed $key
- * @param optional $raw
* @return hash
*/
- public function hash_hmac_file ($file, $key, $raw = Null) {
- return hash_hmac_file($this-algo, $file, $key, $raw);
+ 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);
+ }
}
/**
*
* @param hash $hash
* @param mixed $data
- * @param optional $raw
- * @param optional $hmac
* @return boolean
*/
- public function compare ($hash, $data, $raw = NULL, $hmac = NULL) {
- $newhash = "";
- if (!empty($hmac)) {
- $newhash = $this->hash_hmac($data, $key, $raw);
- } else {
- $newhash = $this->hash($data, $raw);
- }
+ public function compare ($hash, $data) {
+ $newhash = $this->hash($data);
if ($newhash == $hash) {
return true;
}
*
* @param hash $hash
* @param mixed $file
- * @param optional $raw
- * @param optional $hmac
* @return boolean
*/
- public function compare_file ($hash, $file, $raw = NULL, $hmac = NULL) {
- $newhash = "";
- if (!empty($hmac)) {
- $newhash = $this->hash_hmac_file($file, $key, $raw);
- } else {
- $newhash = $this->hash_file($file, $raw);
- }
+ public function compare_file ($hash, $file) {
+ $newhash = $this->hash_file($file);
if ($newhash == $hash) {
return true;
}