Hash.php
00001 <?php
00009 class Dsao_Hash
00010 {
00014 protected $_algorithm = 'md5';
00015
00019 protected $_hash = null;
00020
00024 protected static $_hashInstalled = true;
00025
00030 protected $_length = 0;
00031
00035 protected $_readOnly = false;
00036
00045 protected function __construct($hash = null, $length = 0, $algorithm = null)
00046 {
00047
00048 if (null === $algorithm)
00049 {
00050 $algorithm = Dsao_Registry::get('config')->encryption->hashAlgorithm;
00051 }
00052
00053
00054 $this->setAlgorithm($algorithm);
00055 $this->setHash($hash);
00056 $this->setLength($length);
00057 }
00058
00064 public function __toString()
00065 {
00066 return $this->getHash();
00067 }
00068
00075 protected function _generateHash($text = null)
00076 {
00077 $text = (null === $text ? uniqid(mt_rand(), true) : $text);
00078
00079 $this->_hash = (self::_isHashInstalled() ?
00080 hash($this->_algorithm, $text) :
00081 call_user_func($this->_algorithm, $text));
00082 }
00083
00089 protected static function _isHashInstalled()
00090 {
00091 if (null === self::$_hashInstalled)
00092 {
00093 self::$_hashInstalled = (function_exists('hash') ? true : false);
00094 }
00095
00096 return self::$_hashInstalled;
00097 }
00098
00112 public function compareTo(Dsao_Hash $hash)
00113 {
00114 return ($this->getHash() == $hash->getHash($this->getLength()));
00115 }
00116
00124 public static function factory($hash = null, $length = 0)
00125 {
00126 return new self($hash, $length);
00127 }
00128
00139 public static function fromText($text = null, $length = 0)
00140 {
00141
00142 $hash = new self(null, $length);
00143
00144
00145 $hash->_generateHash($text);
00146
00147 return $hash;
00148 }
00149
00155 public function getAlgorithm()
00156 {
00157 return $this->_algorithm;
00158 }
00159
00167 public function getHash($length = 0)
00168 {
00169
00170
00171 if (0 === $length || (!$length && !$this->_length))
00172 {
00173 return $this->_hash;
00174 }
00175
00176 else if (!$length)
00177 {
00178 $length = $this->_length;
00179 }
00180
00181
00182 return substr($this->_hash, 0, $length);
00183 }
00184
00191 public function getLength($trueLength = false)
00192 {
00193
00194
00195 if ($trueLength || !$this->_length)
00196 {
00197 return strlen($this->_hash);
00198 }
00199
00200 return $this->_length;
00201 }
00202
00209 public function isReadOnly($throw = false)
00210 {
00211 if (true === $throw && true === $this->_readOnly)
00212 {
00213 throw new Dsao_Exception('dsao_hash_object_readonly');
00214 }
00215
00216 return (bool) $this->_readOnly;
00217 }
00218
00228 public function setAlgorithm($algorithm)
00229 {
00230
00231 $this->isReadOnly(true);
00232
00233
00234 if (self::_isHashInstalled())
00235 {
00236
00237 if (in_array($algorithm, hash_algos()))
00238 {
00239 $this->_algorithm = $algorithm;
00240 }
00241 }
00242
00243
00244 else
00245 {
00246 if (function_exists($algorithm))
00247 {
00248 $this->_algorithm = $algorithm;
00249 }
00250 }
00251
00252 return $this;
00253 }
00254
00265 public function setHash($hash = null, $fromText = false)
00266 {
00267
00268 $this->isReadOnly(true);
00269
00270
00271 if (null === $hash || $fromText)
00272 {
00273 $this->_generateHash(($fromText ? $hash : null));
00274 }
00275 else
00276 {
00277 $this->_hash = $hash;
00278 }
00279
00280 return $this;
00281 }
00282
00289 public function setLength($length = 0)
00290 {
00291
00292 $this->isReadOnly(true);
00293
00294 $this->_length = $length;
00295
00296 return $this;
00297 }
00298
00305 public function setReadOnly($readOnly = true)
00306 {
00307 $this->_readOnly = (bool) $readOnly;
00308
00309 return $this;
00310 }
00311 }