Skip to content

Instantly share code, notes, and snippets.

@asfo
Last active November 9, 2015 01:31
Show Gist options
  • Save asfo/3b70dc65629636fe7e3c to your computer and use it in GitHub Desktop.
Save asfo/3b70dc65629636fe7e3c to your computer and use it in GitHub Desktop.
<?php
class PwdImg{
private $password;
private $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
private $long;
public function __construct($pwd){
$this->long = strlen($this->chars);
$this->password = $pwd;
}
public function compare_pwd($pwd){
if($this->read_image($pwd) === $this->_count_chars($this->password))
return true;
else
return false;
}
public function pwd_img($pass){
$this->create_image($this->_count_chars(hash('sha256', $pass)));
}
public function _count_chars($string){
$arr = array();
for($i = 0; $i < $this->long; $i++){
$regex = '/['.$this->chars[$i].']/i';
$arr[$this->chars[$i]] = preg_match_all($regex, $string, $matches);
}
return $arr;
}
public function create_image($arr){
header("Content-Type: image/png");
$im = imagecreate($this->long, $this->long);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 255, 255, 255);
for($y = 0; $y < $this->long; $y++){
$x = $arr[$this->chars[$y]];
imagesetpixel($im, $x, $y, $text_color);
}
imagepng($im);
imagedestroy($im);
}
public function read_image($image){
$arr = array();
$img = imagecreatefrompng($image);
for($y = 0; $y < $this->long; $y++){
for($x = 0; $x < $this->long; $x++){
if(imagecolorat($img, $x , $y)== 1){
$arr[$this->chars[$y]] = $x;
}
}
}
return $arr;
}
}
$connection = new mysqli("localhost", "root", "", "pwd");
$q = $connection->query("SELECT * FROM usuario WHERE usuario = 'asfo' LIMIT 1");
$res = $q->fetch_object();
$a = new PwdImg($res->contrasena);
//$a->pwd_img("helloworld1");
if($a->compare_pwd("pwd/pwd.png")){
echo "Bien";
}else{
echo "Mal";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment