Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got the right idea with the security, but if you're generating a new random salt on every attempt when you're trying to compare with an existing record it <em>should</em> never match (it's random)*. You have to store the <code>salt</code> in the table along with the <code>salted hashed password</code>. The salt should be random for each user (as in random, not their user agent), but you'll need to use the <code>salt</code> from the creation of the account to <code>hash</code> the user's password input for comparison.</p> <p>To make the fix:</p> <ol> <li><p>You'll need to add a 'salt' column to your accounts table (or a relation table that you can relate to the accounts table).</p></li> <li><p>Once that's added then you would need to adjust any of the functions where you're doing an <code>insert</code> into the database with the user's information. You'll need to add <code>salt='$salt'</code> to the SQL.</p></li> <li><p>On the checks for input <code>login_check()</code> and <code>login()</code> you'll need to do the following: </p></li> </ol> <p>Update the SQL for the query to check the username first:</p> <pre><code>"select count(*) from accounts where username = '$username'" </code></pre> <p>If the count is '1', then do this:</p> <pre><code>"select salt from accounts where username = '$username'" </code></pre> <p>Then you would do this:</p> <pre><code>$password_for_comparison = hash('sha512', $password.$salt_from_database); </code></pre> <p>Then you would do this:</p> <pre><code>"select count(*) from accounts where username='username' and password='$password_for_comparison'" </code></pre> <p>If this count is <code>1</code> then you have a valid user.</p> <p>Those few things can be added into a new function and called in the other functions (so you're not maintaining two separate code sets).</p> <p><em><strong>*Note:</em></strong> There are cases where a collision may occur, but with your selection of sha512 this is such a random possibility it's unlikely without someone trying brute force the thing with something like hashcat on a box with 25 vid cards running opencl.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload