How do I implement salt into my login for passwords?

前端 未结 8 1396
醉酒成梦
醉酒成梦 2020-12-12 16:16

I want to implement a salt into my login system but am a bit confused on how this is supposed to work. I can\'t understand the logic behind it. I understand md5 is a one-way

相关标签:
8条回答
  • 2020-12-12 16:53

    As you mentioned, hashing algorithms work only one-way (or only if they are strong enough :-D)

    For your question about salting I would recommend to hash a password with a static salt string and some dynamic data from database, which should not change after once created

    This is a very secure way of storing passwords, as even if database is compromised, hackers/crackers still need to get your static string hash and need to guess how you applied all the salting..

    For example let's say you have a users table with these columns:

    id
    username
    password
    created_at
    

    columns id and created_at after once filled should never be changed..

    so when you are hashing user's password you can do as simple as:

    <?php
        $staticSalt = '!241@kadl;ap][';
        $userPass = 'my new pass';
        // assuming $user variable is already populated with DB data
        // we will generate new hash from columns and static salt:
        $genPass = sha1($user['id'] . $userPass . $user['created_at'] . $staticSalt);
    ?>
    

    I hope this one helps :) cheers

    0 讨论(0)
  • 2020-12-12 16:55

    Forget about using salts (partly for the reason you mention), use bcrypt instead:

    For a good explanation see: http://codahale.com/how-to-safely-store-a-password/

    0 讨论(0)
提交回复
热议问题