ノノログ

ホーム / ブログ / codeIQでPHPのパスワード作成に挑戦したので晒す

codeIQでPHPのパスワード作成に挑戦したので晒す

charConvert();
}

private function charConvert(){
$this->lowerCharAry = str_split($this->lowerChar);
$this->upperCharAry = str_split($this->upperChar);
$this->numAry       = str_split($this->num);
$this->markAry      = str_split($this->mark);

$this->mix = $this->lowerChar . $this->upperChar . $this->num . $this->mark;
}

public function generate() {
$stringSize   = strlen($this->mix);

while(!$this->validate($password)){
$passwordSize = rand(DefaultPassword::PASS_MIN_SIZE, DefaultPassword::PASS_MAX_SIZE);
$password = "";
for ($i = 0; $i < $passwordSize; $i++){
$password .= $this->mix[mt_rand(0, ($stringSize - 1))];
}
}
return $password;
}

public function validate($password) {
// サイズチェック
if (strlen($password) < DefaultPassword::PASS_MIN_SIZE) return false;
if (strlen($password) > DefaultPassword::PASS_MAX_SIZE) return false;

// 強度チェック
$passwordAry = str_split($password);
$point = [0,0,0,0];
// IF YOUR PHP VERSION IS DONW PHP5.3. $point = array(0,0,0,0);
foreach ($passwordAry as $char){
if (array_sum($point) >= 3) return true;
if (!$point[0] && in_array($char, $this->lowerCharAry)) {$point[0] = 1; continue;}
if (!$point[1] && in_array($char, $this->upperCharAry)) {$point[1] = 1; continue;}
if (!$point[2] && in_array($char, $this->numAry))       {$point[2] = 1; continue;}
if (!$point[3] && in_array($char, $this->markAry))      {$point[3] = 1; continue;}
}


if (array_sum($point) >= 3) return true;
return false;
}


public static function get(){
if (!$self) $self = new DefaultPassword();
return $self->generate();
}

}








このエントリーをはてなブックマークに追加