AW: das php script class.password.php ausführen?
Hallo!
Der Quelltext:
<?php
class Password {
private $uniqueLetters = 'abcdefghkmnpqrstuvwxyz';
private $uniqueNumbers = '123456789';
private $uniqueConsonants = 'bcdfghkmnpqrstvwxyz';
private $uniqueVowels = 'aeu';
private $allLetters = 'abcdefghijklmnopqrstuvwxyz';
private $allNumbers = '1234567890';
private $allConsonants = 'bcdfghjklmnpqrstvwxyz';
private $allVowels = 'aeiou';
private $specialChars = '!$%&=?/*-+#;:';
private $lChars = 3;
private $uChars = 2;
private $nChars = 2;
private $sChars = 1;
private $minLength = 8;
private $lCharsRequired = true;
private $uCharsRequired = true;
private $nCharsRequired = true;
private $sCharsRequired = true;
private $isMnemonic = false;
private $useUniqueChars = true;
private $useSalt = true;
private $salt = '';
private $password = '';
private $hash = '';
/** switch on/off generating mnemonic passwords
**************************************************************************************************************
* @param boolean $value
*/
public function isMnemonic($value) {
$this->isMnemonic = $value;
}
/** [do not] allow similar chars in generated passwords
**************************************************************************************************************
* @param boolean $value
*/
public function useUniqueChars($value) {
$this->useUniqueChars = $value;
}
/** [do not] use salt
**************************************************************************************************************
* @param boolean $value
*/
public function useSalt($value) {
$this->useSalt = $value;
}
/** sets the salt
**************************************************************************************************************
* @param string $salt
*/
public function setSalt($salt) {
$this->salt = $salt;
}
/** creates a salt
**************************************************************************************************************
*/
public function createSalt() {
$this->salt = sha1(uniqid(mt_rand(), true));
}
/** returns the salt
**************************************************************************************************************
* @return string
*/
public function getSalt() {
return $this->salt;
}
/** sets the password
**************************************************************************************************************
* @param string $password
*/
public function setPassword($password) {
$this->password = $password;
}
/** generates a password
**************************************************************************************************************
* @param integer $lChars //-> lower case
* @param integer $uChars //-> upper case
* @param integer $nChars //-> numbers
* @param integer $sChars //-> special chars
*/
public function generatePassword($lChars = null, $uChars = null, $nChars = null, $sChars = null) {
if($this->useUniqueChars == true):
$letters = $this->uniqueLetters;
$numbers = $this->uniqueNumbers;
$consonants = $this->uniqueConsonants;
$vowels = $this->uniqueVowels;
else:
$letters = $this->allLetters;
$numbers = $this->allNumbers;
$consonants = $this->allConsonants;
$vowels = $this->allVowels;
endif;
$specialChars = $this->specialChars;
if($lChars === null) $lChars = $this->lChars;
if($uChars === null) $uChars = $this->uChars;
if($nChars === null) $nChars = $this->nChars;
if($sChars === null) $sChars = $this->sChars;
$this->password = '';
if($this->isMnemonic == true):
$maxConsonants = strlen($consonants) - 1;
$maxVowels = strlen($vowels) - 1;
for($i = 1, $j = ceil(($lChars + $uChars + $sChars) / 2); $i <= $j; $i++):
$this->password .= $consonants[mt_rand(0, $maxConsonants)];
$this->password .= $vowels[mt_rand(0, $maxVowels)];
endfor;
$maxNumbers = strlen($numbers) - 1;
for($i = 1; $i <= $nChars; $i++) $this->password .= $numbers[mt_rand(0, $maxNumbers)];
else:
$maxLetters = strlen($letters) - 1;
$maxNumbers = strlen($numbers) - 1;
$maxSpecialChars = strlen($specialChars) - 1;
for($i = 1; $i <= $lChars; $i++) $this->password .= $letters[mt_rand(0, $maxLetters)];
for($i = 1; $i <= $uChars; $i++) $this->password .= strtoupper($letters[mt_rand(0, $maxLetters)]);
for($i = 1; $i <= $nChars; $i++) $this->password .= $numbers[mt_rand(0, $maxNumbers)];
for($i = 1; $i <= $sChars; $i++) $this->password .= $specialChars[mt_rand(0, $maxSpecialChars)];
$this->password = str_shuffle($this->password);
endif;
}
/** returns the password
**************************************************************************************************************
* @return string
*/
public function getPassword() {
return $this->password;
}
/** validates a password
**************************************************************************************************************
* @param boolean $returnDetails
* @param integer $minLength
* @param boolean $lCharsRequired
* @param boolean $uCharsRequired
* @param boolean $nCharsRequired
* @param boolean $sCharsRequired
* @return mixed
*/
public function validatePassword($returnDetails = false, $minLength = null, $lCharsRequired = null,
$uCharsRequired = null, $nCharsRequired = null, $sCharsRequired = null) {
if($minLength === null) $minLength = $this->minLength;
if($lCharsRequired === null) $lCharsRequired = $this->lCharsRequired;
if($uCharsRequired === null) $uCharsRequired = $this->uCharsRequired;
if($nCharsRequired === null) $nCharsRequired = $this->nCharsRequired;
if($sCharsRequired === null) $sCharsRequired = $this->sCharsRequired;
$aValidation = array('length' => true, 'lChars' => true, 'uChars' => true, 'nChars' => true, 'sChars' => true);
if($minLength > 0 && strlen($this->password) < $this->minLength) $aValidation['length'] = false;
if($lCharsRequired == true && !preg_match('/[a-z]/', $this->password)) $aValidation['lChars'] = false;
if($uCharsRequired == true && !preg_match('/[A-Z]/', $this->password)) $aValidation['uChars'] = false;
if($nCharsRequired == true && !preg_match('/[0-9]/', $this->password)) $aValidation['nChars'] = false;
if($sCharsRequired == true && !preg_match('/[^A-Za-z0-9]/', $this->password)) $aValidation['sChars'] = false;
if($returnDetails == true) return $aValidation;
return array_search(false, $aValidation, true) !== false ? false : true;
}
/** sets the password hash
**************************************************************************************************************
* @param string $hash
*/
public function setHash($hash) {
$this->hash = $hash;
}
/** creates the password hash
**************************************************************************************************************
*/
public function makeHash() {
if($this->useSalt == false): $this->hash = sha1($this->password);
else: $this->hash = sha1($this->salt.sha1($this->salt.$this->password));
endif;
}
/** returns the password hash
**************************************************************************************************************
* @return string
*/
public function getHash() {
return $this->hash;
}
}
?>