First, by answering the question. Yes, it is possible, if the input value is equal to the existing value in the database before it is encrypted.
Encrypting the input value and compared to the existing value, you get the same return.
function senha($arg){
$md5 = md5($arg, true);
return substr($md5, 0, 22);
}
print senha('1234');
print "<br/>";
print senha('1234');
$senha_armazenada = senha('1234');
if($senha_armazenada === senha('1234')){
print "senha confere";
} else {
print "senha nao confere";
}
However cryptographic functions such as md5, sha1
and hereafter, are considered unfit for such tasks as "easy" to break. Although they seem indestructible, nowadays there is immense processing power and immense techniques to get the true value of this hash.
To create a hash
safe, currently there are 2 important factors to take into account:
- The cost (time the computer will take to generate this hash).
- salt (a unique increment, which makes hash unique for each case).
The PHP >= 5.5.0
possesses native functions to create, check safe hashes without much effort. For older versions PHP < 5.5.0
, there are ways to get the same result, also explained here.
Instead of using md5
, you can use the native functions of PHP >= 5.5.0
to create something more secure.
<?php
header("Contet-Type: text/html; charset=utf-8;");
$usuarios = array(
0 => array(
'id'=>1,
'nome'=>'Edilson',
'hash'=>'$2y$10$i260FJQg7VgsNjXl6s9Mje9aqXUGbfa9L/c8bA2NOUHyDVoyJoyQu'
),
1 => array(
'id'=>1,
'nome'=>'Samuel',
'hash'=>'$2y$10$r1wD4rLLgB1jm6ExF.Em5eyKXdK4Wn8f6z.G9fsxmc3xXay4.pI/O'
)
);
function logar($usuario, $senha){
global $usuarios;
foreach($usuarios as $key=>$set){
if(in_array($usuario, $set)){
if(password_verify($senha, $set['hash'])){
return true;
}
}
}
return false;
}
function cadastrar($usuario, $senha){
global $usuarios;
if(!empty($usuario) && !empty($senha)){
$hash = password_hash($senha, PASSWORD_BCRYPT);
$id = mt_rand(3,50);
if(array_push($usuarios, array('id'=>$id, 'nome'=>$usuario, 'hash'=>$hash))){
return true;
}
}
return false;
}
//var_dump(cadastrar('Edilson','password')); # (true/cadastrado)
var_dump(logar('Edilson', 'password')); # (true/logado)
var_dump(logar('Samuel', '1234')); # (true/logado)print_r($usuarios);
?>
Here for example, the variable $usuarios
functions as a table in the database, and when the function logado
is called, it looks in this array, the corresponding hash, and compares, through the function password_verify
, which returns true if both are equal, or false if the comparison fails.
Recommended:
In the update the correct would not be to request the password for the user as confirmation? Bringing from the bank is not a good idea. MD5 is no longer safe either, due to dictionary attacks, it might be worth considering using another method.
– George Wurthmann
The ideal is not to display the filled password, I usually leave the password field locked and a checkbox to enable this field, if he wants to update the password, he registers a new password.
– Jeferson Assis
@Georgewurthmann an Administrator user could change the password of a lower level user without the need to know the password of the user. what other method you would indicate ?
– Gabriel Rodrigues
On any site, the password update form comes with empty value or a fixed amount of asterisks. What you want to do of displaying the amount of characters in the password is a security flaw. Related issue: How to decrypt MD5?
– Pedro Sanção
@Jefersonassis I wish I could show the "****" various systems do this, I just don’t know which approach is the most assertive in this case, on the question of identifying when to change the password is simple, if someone focuses on her I delete it so that he can type in a new one and if it doesn’t change...
– Gabriel Rodrigues
@Penalty then they put a fixed amount, regardless of password size ?
– Gabriel Rodrigues
@Gabrielrodrigues exactly, but leave the field empty is the most used, check for example the form to change password here Sopt
– Pedro Sanção
@Gabrielrodrigues the administrator should not even know the password, only the user should know his own password. The administrator can change the password in the same way as the user resets it. The previous password is 'forgotten' and a new one is assigned. But when I referred to the method, I was talking about cryptography, in this case I indicate that I use Bcrypt. Read more in this question by Sopt: http://answall.com/questions/2402/como-fazer-hash-de-passwords_safesafesafesafesafest
– George Wurthmann
It makes no difference to insert the asterisks. It doesn’t matter what the X site or Y site does.. Normally, serious website that does this merely places some asterisks to illustrate. It is merely visual but has no need whatsoever. And another point they commented on, don’t give any chance for a clue that leads a hacker to understand what the password looks like. If the subject knows that the password has 5 or 8 characters, It is less complicated to break the password because nor will waste time with other sizes, understood?
– Daniel Omine
@Danielomine, I understand, the answers were able to answer these questions.
– Gabriel Rodrigues