What is md5() for?

前端 未结 13 1201
無奈伤痛
無奈伤痛 2021-01-31 01:57

I was reading this tutorial for a simple PHP login system.

In the end it recommends that you should encrypt your password using md5().

Though I know this is a be

13条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 02:25

    You should have an encryption like md5 or sha512. You should also have two different salts, a static salt (written by you) and then also a unique salt for that specific password.

    Some sample code (e.g. registration.php):

    $unique_salt = hash('md5', microtime()); 
    $password = hash('md5', $_POST['password'].'raNdoMStAticSaltHere'.$unique_salt);
    

    Now you have a static salt, which is valid for all your passwords, that is stored in the .php file. Then, at registration execution, you generate a unique hash for that specific password.

    This all ends up with: two passwords that are spelled exactly the same, will have two different hashes. The unique hash is stored in the database along with the current id. If someone grab the database, they will have every single unique salt for every specific password. But what they don't have is your static salt, which make things a lot harder for every "hacker" out there.

    This is how you check the validity of your password on login.php for example:

    $user = //random username;
    $querysalt = mysql_query("SELECT salt FROM password WHERE username='$user'");
    while($salt = mysql_fetch_array($querysalt)) {
        $password = hash('md5',
              $_POST['userpassword'].'raNdoMStAticSaltHere'.$salt[salt]);
    }
    

    This is what I've used in the past. It's very powerful and secure. Myself prefer the sha512 encryption. It's actually just to put that inside the hash function instead of md5 in my example.

    If you wanna be even more secure, you can store the unique salt in a completely different database.

提交回复
热议问题