Proper way to sanitize a password?
Solution 1
If you are going to put the variable in an SQL query, then you either need to call mysqli_read_escape_string
or (even better!) use prepared statements.
There's no other sanitization you need to do. However, if the value will be coming from freeform user input (e.g. a text box instead of a drop down menu) then you may also want to trim whitespace and lowercase it as a courtesy to the user (to correct accidental mistakes they might make). It really depends on the application.
Solution 2
Just to be clear, you're receiving from an un-trusted source a hash (effectively random data) + salt (actually random data), and you want to 'sanitize' it? There is probably a definition of sanity that applies (a data format like base64 encoding, a maximum / expected length), but I strongly suspect there is a functional security mistake in there somewhere.
Most notably, why are you accepting a hash+salt from an un-trusted source, rather than accepting a password and doing the transformation within your trusted environment? Accepting a hash+salt from an un-trusted source probably turns them into plain-text equivalents (you lose the benefit you got from hashing and salting the original password).
daniel__
Founder of Teamlyzer Something between Glassdoor and Stackoverflow for portuguese speakers.
Updated on June 28, 2022Comments
-
daniel__ almost 2 years
How I can sanitize a string that receives a hash+random salt?
I can remove the white spaces, check the length and use mysqli_real_escape_string, but it sufficient? The filter_var is really useful but it can't help in this case, right?