Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In fact password encryption has nothing in common with mysql injection. You should (MUST!) do theese things separately.</p> <p>Password encryption (or in this case hashing) will make it unable for possible hacker who gained access to your databse to use your users passwords. Hash functions are one way - you can create hash from password, but no password from hash. The only way to get password from hash is to hash every possible combination of chars and check whether this hash equals to this in db. Most uf users uses simple md5() or sha256() functions in php. In fact it works, but the problem with those functions is their simplicity. The were created to calculate check-sums of files, so they must have been fast. Thats why it makes it easier to brute-force. To avoid being brute-forced you can:</p> <p>a) Add 'salt' and keep using md5 / sha256. Salt is a random string added to user password before hashing. You must create additional column in your databse, near 'password' for example. Each user should get random, at least 32 chars long salt. The 'password' field is created using md5(salt . users_password). If you want to check the password on login - get the password and salt fields from databse, get users password from post and compare: md5(salt . user_password_from_post) to 'password' in databse. Even if users password is short it becomes long and complex because of the salt. To crack / brute force all 8-chars password you need only ~80^8 hashes, but to brute force salted 8-chars password you need ~80^40 which is 80^32 times longer.</p> <p>b) use blowfish algorithm <a href="http://php.net/manual/en/function.crypt.php" rel="nofollow">http://php.net/manual/en/function.crypt.php</a> The blowfish was created from the beggining to crypt passwords. You also have to use salt, but you also can specify 'cost' parameter which indicates how complex would be the hash. More complex = more cpu usage on every password check but also more safety. Password secured with blowfish, 16-bytes salt and at least 10 cost are said to be safe.</p> <p>To avoid being sql-injected you should escape every parameter passed to your query. mysql_real_escape_string() becomes your friend.</p> <pre><code>$Query = sprintf("SELECT UserId, Password, Salt FROM Users WHERE Username='%s'", mysql_real_escape_string($_POST['Username'])); mysql_query($Query); </code></pre> <p>If the</p> <pre><code>$_POST['Username'] = "'; DROP TABLE Users; --" </code></pre> <p>your query without escaping would become:</p> <pre><code>"SELECT UserId, Password, Salt FROM Users WHERE Username=''; DROP TABLE Users; --' </code></pre> <p>by this query any user can destroy your database without any problem. With mysql_real_escape_string, query would look loke:</p> <pre><code>"SELECT UserId, Password, Salt FROM Users WHERE Username='\'; DROP TABLE Users; --' </code></pre> <p>Now your database is safe.</p> <p>The complete code for checking password (you should have username(128), password(32), salt(32) in DB):</p> <pre><code>function CheckPassword($Username, $Password) { list($Count) = mysql_fetch_array(mysql_query(sprintf("SELECT COUNT(*) FROM Users WHERE Username='%s'", mysql_real_escape_string($Username)))); if(!$Count) return false; //No such user list($Password_Db, $Salt) = mysql_fetch_array(mysql_query(sprintf("SELECT Password, Salt FROM Users WHERE Username='%s'", mysql_real_escape_string($Username)))); if(md5($Salt . $Password) == $Password_Db) return true; return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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