The password_hash
supports Bcrypt and Argon2i (in PHP 7.2). MD5 has never been assigned to passwords. Moreover since 1994 it could already be considered broken, nowadays it is not recommended for anything, but that is not the issue here.
So that you use the password_verify
it is preferable to use a password compatible with Bcrypt (or Argon2i in the case of PHP 7.2), you can use:
To Bcrypt:
password_hash($senha, PASSWORD_BCRYPT);
To Argon2i:
password_hash($senha, PASSWORD_ARGON2I);
/!\ Care:
The password_hash
does not remove the nulls and will stop at them, so this is broken:
// Não utilize o código abaixo em produção, existem erros intencionais:
$_POST['senha'] = "a\x00bc";
// Nota: Existe um nulo após o `a`, isso pode ser enviado usando `%00` pelo usuário!
$hash = password_hash($_POST['senha'], PASSWORD_BCRYPT);
if(strlen($_POST['senha']) >= 3 && strlen($_POST['senha']) < 70){
if( password_verify('a', $hash)){
echo 'Igual';
}
}
Test this.
Upshot: Igual
, yes the a
is equal to a\x00bc
. >:D
If you don’t want to use Bcrypt/Agon2i all is not lost, you can use the PBKDF2
, for example:
$senha = '12345678';
$salt = random_bytes(16);
$iteracoes = 150000;
$hash = hash_pbkdf2('sha3-512', $senha, $salt, $iteracoes);
To check just do hash_equals($senha, $hash)
. Never do $senha === $hash
let alone do $senha == $hash
, obviously. It is not considered better than Bcrypt and much less better than Argon2i, some say it is the "worst of the recommended methods", being used "when there is nothing better".
password_verify only works with password_hash http://php.net/manual/en/function.password-hash.php
– Felipe Duarte