hash pdo php sql

Cleansing User Passwords


How should I escape or cleanse user-provided passwords before I hash them and store them in my database?

When PHP developers consider hashing users’ passwords for security purposes, they often tend to think of those passwords like they would any other user-provided data. This subject comes up often in PHP questions related to password storage; the developer often wants to cleanse the password using functions such as escape_string()(in various iterations), htmlspecialchars(), addslashes() and others before hashing it and storing it in the database.


  • 1

    u can user base64 encode

    – MSS

    Nov 7, 2017 at 5:01

  • No @MSS, you shouldn’t because base64 is encoding, not encrypting or hashing. Passwords should always be hashed.

    Nov 7, 2017 at 12:33

  • 1

    I mean before hash 😉

    – MSS

    Nov 8, 2017 at 6:01

  • You shouldn’t and do not need to do that before hashing. It’ll cause you to have to write unnecessary additional code @MSS

    Nov 8, 2017 at 13:12


You should never escape, trim or use any other cleansing mechanism on passwords you’ll be hashing with PHP’s password_hash() for a number of reasons, the single largest of which is because doing additional cleansing to the password requires unnecessary additional code.

You will argue (and you see it in every post where user data is accepted for use in your systems) that we should cleanse all user input and you would be right for every other piece of information we’re accepting from our users. Passwords are different. Hashed passwords cannot offer any SQL injection threat because the string is turned into hash prior to storing in the database.

The act of hashing a password is the act of making the password safe to store in your database. The hash function doesn’t give special meaning to any bytes, so no cleansing of its input is required for security reasons

If you follow the mantras of allowing users to use the passwords / phrases they desire and you don’t limit passwords, allowing any length, any number of spaces and any special characters hashing will make the password/passphrase safe no matter what is contained within the password. As of right now the most common hash (the default), PASSWORD_BCRYPT, turns the password into a 60 character wide string containing a random salt along with the hashed password information and a cost (the algorithmic cost of creating the hash):

PASSWORD_BCRYPT is used to create new password hashes using the CRYPT_BLOWFISH algorithm. This will always result in a hash using the “$2y$” crypt format, which is always 60 characters wide.

The space requirements for storing the hash are subject to change as different hashing methods are added to the function, so it is always better to go larger on the column type for the stored hash, such as VARCHAR(255) or TEXT.

You could use a complete SQL query as your password and it would be hashed, making it unexecutable by the SQL engine e.g.,

SELECT * FROM `users`;

Could be hashed to $2y$10$1tOKcWUWBW5gBka04tGMO.BH7gs/qjAHZsC5wyG0zmI2C.KgaqU5G

Let’s see how different sanitizing methods affect the password –

The password is I'm a "dessert topping" & a <floor wax>! (There are 5 spaces at the end of the password which are not displayed here.)

When we apply the following methods of trimming we get some wildy different results:



string(40) "I'm a "dessert topping" & a <floor wax>!" // spaces at the end are missing
string(65) "I'm a &quot;dessert topping&quot; &amp; a &lt;floor wax&gt;!     " // double quotes, ampersand and braces have been changed
string(65) "I'm a &quot;dessert topping&quot; &amp; a &lt;floor wax&gt;!     " // same here
string(48) "I\'m a \"dessert topping\" & a <floor wax>!     " // escape characters have been added
string(34) "I'm a "dessert topping" & a !     " // looks like we have something missing

What happens when we send these to password_hash()? They all get hashed, just as the query did above. The problem comes in when you try to verify the password. If we employ one or more of these methods we must re-employ them prior to comparing them with password_verify(). The following would fail:

password_verify($_POST['upassword'], $hashed_password); // where $hashed_password comes from a database query

You would have to run the posted password through the cleansing method you chose before using the result of that in password verification. It is an unnecessary set of steps and will make the hash no better.

Using a PHP version less than 5.5? You can use the password_hash() compatibility pack.

You really shouldn’t use MD5 password hashes.


  • 13

    No. If he created his password with trailing spaces, which is allowed, he must use them on login @DanBracuk

    Apr 14, 2016 at 16:19

  • 12

    How so @DanBracuk? If we allow the user to setup the password s/he desires, including leading/trailing spaces?

    Apr 14, 2016 at 16:22

  • 16

    That’s why most things require you to enter your chosen password twice. If user added the spaces on accident they will figure it out before getting any further. If user did it on purpose than it’s a non-issue.

    Apr 14, 2016 at 16:24

  • 4

    @MargaretBloom, a rule of thumb is just a heuristic. We sometimes still need to think things through, like for passwords. You say “nobody knows how things will change in the future”, but it seems if anything is going to change it’s the way we escape data before we put it into the database, in which cases users would find themselves locked out when their passwords no longer match what we’ve stored. What is the danger in not escaping password hashes vs. the danger of escaping them?

    – DavidS

    Apr 14, 2016 at 23:05

  • 3

    Exactly: you will of course “escape the hash” in the limited sense of correctly passing it to a parameterized SQL query, where some code in your SQL connector may or may not do anything with it that corresponds to “escaping”, you don’t know and don’t care. You just won’t have to write any specific code to achieve that, because it’s completely routine for all your SQL queries unless you’ve previously made some poor life decisions.

    Apr 14, 2016 at 23:25


Before hashing the password, you should normalise it as described in section 4 of RFC 7613. In particular:

  1. Additional Mapping Rule: Any instances of non-ASCII space MUST be
    mapped to ASCII space (U+0020); a non-ASCII space is any Unicode
    code point having a Unicode general category of “Zs” (with the
    exception of U+0020).


  1. Normalization Rule: Unicode Normalization Form C (NFC) MUST be
    applied to all characters.

This attempts to ensure that if the user types the same password but using a different input method, the password should still be accepted.


  • 3

    @DavidS, A super shiny North american Mac Book (that Joe used just before leaving) and a poorly internationalized Taiwanese internet café computer (that Joe is trying to use to download is flight back boarding card).

    Apr 14, 2016 at 22:15

  • 2

    Sounds jingoistic. 🙂 Thanks though.

    – DavidS

    Apr 14, 2016 at 22:46

  • 3

    Hmm. If you do this, then you should also validate passwords to reject any that contain as-yet-unassigned characters. It would be terrible if a user uses NEWFANGLED SPACE, which your app doesn’t recognize and therefore hashes as-is, and then you upgrade your Unicode Character Database and suddenly NEWFANGLED SPACE gets mapped to SPACE before hashing, such that (s)he can no longer enter a password that your app will hash to the old hash.

    – ruakh

    Apr 14, 2016 at 22:46

  • 1

    Curious – how would a non-Ascii space get entered into the password if the application is UTF-8 all the way through?

    Apr 27, 2016 at 19:39

  • 4

    @JayBlanchard Because when you press a space bar on one machine and when you press it on another machine you might get two different Unicode code points, and they’ll have two different UTF-8 encodings, without the user being aware of anything. It could be argued that this is a problem you wish to ignore, but RFC 7613 was borne out of such real-life issues, it’s not a make-work recommendation.

    Apr 27, 2016 at 20:03