10 defined(
'JPATH_PLATFORM') or die;
31 protected $defaultType =
'$2y$';
44 public function create($password, $type = null)
48 if (strlen($password) > 55)
57 $type = $this->defaultType;
73 $salt = $type . str_pad($this->cost, 2,
'0', STR_PAD_LEFT) .
'$' . $this->getSalt(22);
75 return crypt($password, $salt);
78 $salt = $this->getSalt(12);
80 $salt =
'$1$' . $salt;
82 return crypt($password, $salt);
85 $salt = $this->getSalt(32);
87 return md5($password . $salt) .
':' . $salt;
90 throw new InvalidArgumentException(sprintf(
'Hash type %s is not supported', $type));
104 public function setCost($cost)
118 protected function getSalt($length)
120 $bytes = ceil($length * 6 / 8);
124 return substr($randomData, 0, $length);
137 public function verify($password, $hash)
140 if (substr($hash, 0, 4) ==
'$2a$' || substr($hash, 0, 4) ==
'$2y$')
151 $hash = $type . substr($hash, 4);
153 return (crypt($password, $hash) === $hash);
157 if (substr($hash, 0, 3) ==
'$1$')
159 return (crypt($password, $hash) === $hash);
163 if (preg_match(
'#[a-z0-9]{32}:[A-Za-z0-9]{32}#', $hash) === 1)
165 return md5($password . substr($hash, 33)) == substr($hash, 0, 32);
180 public function setDefaultType($type)
184 $this->defaultType = $type;
195 public function getDefaultType()
197 return $this->defaultType;