Created
February 11, 2013 19:49
-
-
Save alexbonhomme/4757038 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Outils pour la génération et vérification de hash mot de passe | |
* Utilise conjointement sha1() et md5() | |
* | |
* @author Alexandre BONHOMME | |
* @date 11/06/2012 | |
*/ | |
class HashPassword { | |
const SALT_LENGTH = 9; | |
private $pw_hash = NULL; | |
function __construct($passwordHash = NULL) { | |
// Hash du mot de passe | |
$this->pw_hash = $passwordHash; | |
} | |
/** | |
* Vérification du mot de passe | |
* @param Mot de passe en clair | |
* @return True, si le mot de passe est correct | |
*/ | |
public function verifPassword($password) { | |
if( $this->pw_hash === NULL ) { | |
die ( 'Aucun hash trouvé...' ); | |
} | |
// Récupération du hash md5 | |
$salt = substr($this->pw_hash, 0, self::SALT_LENGTH); | |
// Vérification | |
return ( ($salt . sha1($salt . $password)) === $this->pw_hash ); | |
} | |
/** | |
* Génère un hash du mot de passe et le sauvegarde dans l'objet | |
* @param password Mot de passe a hacher | |
* @return Hash du mot passé en paramètre | |
*/ | |
public function generateHash($password) { | |
// Complexification du hash par calcul d'un hash md5 supplémentaire | |
$salt = substr(md5(uniqid(rand(), true)), 0, self::SALT_LENGTH); | |
// Mise à jour du hash | |
return ( $this->pw_hash = ($salt . sha1($salt . $password)) ); | |
} | |
public function setHash($hash = NULL) { | |
$this->pw_hash = $hash; | |
} | |
public function getHash() { | |
if ( $this->pw_hash === NULL ) { | |
echo 'Aucun hash n\'est définit.'; | |
return -1; | |
} else { | |
return $this->pw_hash; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment