[CODE] password system in C language

Discussion in 'Mac Programming' started by brenopop, Jan 4, 2015.

  1. brenopop, Jan 4, 2015
    Last edited: Jan 4, 2015

    brenopop macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #1
    Dear friends, I wanna write a code in C that when the user writes something, doesn't show what they wrote, instead, the program shows only asterisks.
    I can't find a function to do this, because scanf, gets or getchar don`t hide what the user write.

    summarizing, I want to do a login system that doesn't show up what the user write in the password field.
     
  2. brenopop, Jan 4, 2015
    Last edited: Jan 4, 2015

    brenopop thread starter macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #3
    Man, I'm sorry, but I'm not understanding this site. If you could make a simple example to show me, I'll thank you a lot. :)

    ~ Edited ~
    I was looking for this and I found something interesting: the function getch(). BUT, it doesn't work, even with the curses library. Help
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    You can't use getch by itself, curses require you to do some initial setup and cleanup. I assume you are using Terminal so try the getpass function. I reads from the terminal to an internal buffer and turns off character echo to stdout.

    Read more about the function with: man getpass in the terminal.
     
  4. brenopop thread starter macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #5
    Thank you, man. And how can i use curses? this initial setup and cleanup, How can I do it?
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Well, look up some tutorial on curses if you want to use that. I mean you can use noecho with curses, but it uses its own functions to read and write to the screen, after you do the initial setup, depending on what you want. But, I assume you want to use the normal terminal and just get a string without echo to stdout, getpass does that in 1 line.
     
  6. brenopop thread starter macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #7
    Yeah, that's what I want. Thank you so much. :)
     
  7. brenopop thread starter macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #8
    There's a problem now, I don't know how to use getpass() and how to make show up "*" instead the input of keyboard.
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    I just want to mention that having * show up is:

    1 - Less secure, as people watching the screen now know the password length.

    2 - Not normal for a command line program.
     
  9. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #10
    It would be quite easy to create a dialog that would require a user to type a password into a secure text field using XCode and Cocoa.
     
  10. brenopop thread starter macrumors newbie

    Joined:
    Jan 4, 2015
    Location:
    Brazil, Maranhão, São Luís
    #11
    What can I do instead?
    Isn't that the pattern for password in almost everything?

    ----------

    I see, but it is a homework that we suppose to do all of this in C.
    anyway, thank you for help. My next task will be bend Cocoa hehe :D
     
  11. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #12
    No, the normal thing for passwords on a command line are that they don't show anything as you type them.

    If you're making a command line program that needs the user to enter a password, I would suggest making it behave the same as other command line programs and show nothing as the password is entered. To replicate this, just use getpass().

    If you're making a program with a GUI, then those normally show an asterisk for each character entered. You should use the proper password input widget for whatever GUI framework you're using - as others mentioned, in Cocoa the proper widget is the NSSecureTextField.

    But you said this program is in C, which suggests you're probably not going to have a GUI, as programs with GUIs are normally done in Obj-C or C++ instead of pure C (or some other language entirely.)
     

Share This Page