Thread: Password help

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Still, using strcmp() or strncmp() which are standard, would be better. But you've seemed to ignore what people have said about using gets()!

    Use fgets(), no excuses. Your code doesn't work in other words, what's to say I enter a password longer than 20 characters? Oops.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Besides agreeing that you should use fgets() [like I said earlier], you don't need to compare EVER character if you find a "mismatch". Just set "q" to something non-zero and break the loop (using "break;" in the "not equal" part, or by adding "&& !q" to the for-loop condition). It probably doesn't make any difference here, but wriitng code that does more than it needs to is always bad practice.

    Of course, strcmp() would also be a good suggestion, it compares two strings - there's no reason not to trust strcmp(). [1]

    The third thing is of course that password checking should not be done by comparing with a clear-text string - it should be done by storing a hashed value of the password and comparing the hash of the entered password with the stored value. But that is a more advanced topic.

    [1] strncmp is an alternative, however, since at least one of the strings are known length and known to be "wellformed", I don't really see the point of using strncmp in this case.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM