TYPO3 CMS  TYPO3_6-2
All Classes Namespaces Files Functions Variables Pages
Md5Salt.php
Go to the documentation of this file.
1 <?php
3 
27 
32  const ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
38  static protected $saltLengthMD5 = 6;
39 
45  static protected $saltSuffixMD5 = '$';
46 
52  static protected $settingMD5 = '$1$';
53 
60  protected function applySettingsToSalt($salt) {
61  $saltWithSettings = $salt;
62  $reqLenBase64 = $this->getLengthBase64FromBytes($this->getSaltLength());
63  // Salt without setting
64  if (strlen($salt) == $reqLenBase64) {
65  $saltWithSettings = $this->getSetting() . $salt . $this->getSaltSuffix();
66  }
67  return $saltWithSettings;
68  }
69 
78  public function checkPassword($plainPW, $saltedHashPW) {
79  $isCorrect = FALSE;
80  if ($this->isValidSalt($saltedHashPW)) {
81  $isCorrect = crypt($plainPW, $saltedHashPW) == $saltedHashPW;
82  }
83  return $isCorrect;
84  }
85 
97  protected function getGeneratedSalt() {
99  return $this->base64Encode($randomBytes, $this->getSaltLength());
100  }
101 
109  public function getHashedPassword($password, $salt = NULL) {
110  $saltedPW = NULL;
111  if (!empty($password)) {
112  if (empty($salt) || !$this->isValidSalt($salt)) {
113  $salt = $this->getGeneratedSalt();
114  }
115  $saltedPW = crypt($password, $this->applySettingsToSalt($salt));
116  }
117  return $saltedPW;
118  }
119 
125  protected function getItoa64() {
126  return self::ITOA64;
127  }
128 
134  public function isAvailable() {
135  return CRYPT_MD5;
136  }
137 
143  public function getSaltLength() {
144  return self::$saltLengthMD5;
145  }
146 
152  protected function getSaltSuffix() {
153  return self::$saltSuffixMD5;
154  }
155 
161  public function getSetting() {
162  return self::$settingMD5;
163  }
164 
177  public function isHashUpdateNeeded($passString) {
178  return FALSE;
179  }
180 
187  public function isValidSalt($salt) {
188  $isValid = ($skip = FALSE);
189  $reqLenBase64 = $this->getLengthBase64FromBytes($this->getSaltLength());
190  if (strlen($salt) >= $reqLenBase64) {
191  // Salt with prefixed setting
192  if (!strncmp('$', $salt, 1)) {
193  if (!strncmp($this->getSetting(), $salt, strlen($this->getSetting()))) {
194  $isValid = TRUE;
195  $salt = substr($salt, strlen($this->getSetting()));
196  } else {
197  $skip = TRUE;
198  }
199  }
200  // Checking base64 characters
201  if (!$skip && strlen($salt) >= $reqLenBase64) {
202  if (preg_match('/^[' . preg_quote($this->getItoa64(), '/') . ']{' . $reqLenBase64 . ',' . $reqLenBase64 . '}$/', substr($salt, 0, $reqLenBase64))) {
203  $isValid = TRUE;
204  }
205  }
206  }
207  return $isValid;
208  }
209 
216  public function isValidSaltedPW($saltedPW) {
217  $isValid = FALSE;
218  $isValid = !strncmp($this->getSetting(), $saltedPW, strlen($this->getSetting())) ? TRUE : FALSE;
219  if ($isValid) {
220  $isValid = $this->isValidSalt($saltedPW);
221  }
222  return $isValid;
223  }
224 
225 }
checkPassword($plainPW, $saltedHashPW)
Definition: Md5Salt.php:78
static generateRandomBytes($bytesToReturn)
getHashedPassword($password, $salt=NULL)
Definition: Md5Salt.php:109