Created
December 8, 2011 17:20
-
-
Save TiuTalk/1447693 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 | |
/** | |
* Gera um salt aleatório | |
* | |
* @param int $tamanho Tamanho do salt | |
* | |
* @return string | |
*/ | |
function geraSaltAleatorio($tamanho = 22) { | |
return substr(sha1(mt_rand()), 0, $tamanho); | |
} | |
$salt = geraSaltAleatorio(); | |
// Senha do usuário, pode ter vindo do $_POST, $_GET ou outro lugar | |
$senha = 'olá mundo'; | |
// Encripta a senha usando MD5 | |
$senha = md5($senha . $salt); | |
// Resultado: | |
// c1de0ebde1fd59955ccd57ccd89ac2e9 | |
// Salvamos $senha e $salt no banco de dados |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a bad way to generate a salt, and a dreadful way to store a password.
mt_rand will usually only generate only 2^31 distinct values, whilst this is a large number, it is a lot less than you might expect from 22 hex digits (2^88).
So only 2^31 times harder to crack than unsalted MD5.
Use password_hash https://secure.php.net/manual/en/function.password-hash.php