How to use sha256 in php5.3.0

后端 未结 5 1766
时光取名叫无心
时光取名叫无心 2021-02-01 01:32

I\'m using sha256 to encrypt the password. I can save the sha256 encrypted password in mysql. But i can\'t login with the same clause.

Insert code:



        
相关标签:
5条回答
  • 2021-02-01 01:45

    A way better solution is to just use the excelent compatibility script from Anthony Ferrara:

    https://github.com/ircmaxell/password_compat

    Please, and also, when checking the password, always add a way (preferibly async, so it doesn't impact the check process for timming attacks) to update the hash if needed.

    0 讨论(0)
  • 2021-02-01 01:47

    Could this be a typo? (two Ps in ppasscode, intended?)

    $_POST['ppasscode'];
    

    I would make sure and do:

    print_r($_POST);
    

    and make sure the data is accurate there, and then echo out what it should look like:

    echo hash('sha256', $_POST['ppasscode']);
    

    Compare this output to what you have in the database (manually). By doing this you're exploring your possible points of failure:

    1. Getting password from form
    2. hashing the password
    3. stored password
    4. comparison of the two.
    0 讨论(0)
  • 2021-02-01 01:47

    The first thing is to make a comparison of functions of SHA and opt for the safest algorithm that supports your programming language (PHP).

    Then you can chew the official documentation to implement the hash() function that receives as argument the hashing algorithm you have chosen and the raw password.

    sha256 => 64 bits sha384 => 96 bits sha512 => 128 bits

    The more secure the hashing algorithm is, the higher the cost in terms of hashing and time to recover the original value from the server side.

    $hashedPassword = hash('sha256', $password);
    
    0 讨论(0)
  • 2021-02-01 01:54

    First of all, sha256 is a hashing algorithm, not a type of encryption. An encryption would require having a way to decrypt the information back to its original value (collisions aside).

    Looking at your code, it seems it should work if you are providing the correct parameter.

    • Try using a literal string in your code first, and verify its validity instead of using the $_POST[] variable

    • Try moving the comparison from the database query to the code (get the hash for the given user and compare to the hash you have just calculated)

    But most importantly before deploying this in any kind of public fashion, please remember to sanitize your inputs. Don't allow arbitrary SQL to be insert into the queries. The best idea here would be to use parameterized queries.

    0 讨论(0)
  • 2021-02-01 01:59

    You should use Adaptive hashing like http://en.wikipedia.org/wiki/Bcrypt for securing passwords

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