Results 1 to 6 of 6

Thread: Do I need to escape a string if it's being encoded?

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default Do I need to escape a string if it's being encoded?

    Hi, as the title suggests, I have a password that I'm encrypting before it is inserted into the database, do I still need to mysql_real_escape_string it or not?

    Here is the code if you're interested:

    PHP Code:
    $salt substr(sha1(uniqid(rand(), true)), 10); // Generate a unique 10 character salt 
            
    $encpass sha1($salt $_POST['password']); // Encrypt password with salt
            
    $pass $salt $encpass// Salt + Encrypted password, salt is prepended so it can be matched when a user logs in

            
    $query "INSERT INTO admin (user, pass) VALUES ('$user', '$pass')"

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In this case, no, you don't, but only because the return value of sha1() is guaranteed to contain only digits from 0 to F.

    Your idea of a 'salt' is a little wacky. If you ever want to be able to work with the hashed (not encrypted) value again, then you need to be able to regenerate the salt: that means that anything random (like rand()) or unique (like uniqid()) is expressly not what you want, unless you store the results for later retrieval.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ok thanks and yea I'm storing the salt along with the hash and just so you don't think I'm completely crazy I was following the guidelines as stated here:

    http://phpsec.org/articles/2005/password-hashing.html

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You seem to have missed the other feature used there, though: prepared statements.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    What do you mean by prepared statements? The fact he has what I have within a function?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, the method used of inserting values into an existing SQL query. It handles escaping and the like automatically.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •