Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 4, 2015, 10:37 PM   #1
brenopop
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
[CODE] password system in C language

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.

Last edited by brenopop; Jan 4, 2015 at 11:36 PM.
brenopop is offline   0 Reply With Quote
Old Jan 4, 2015, 11:10 PM   #2
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
Take a look at NSSecureTextField.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Jan 4, 2015, 11:28 PM   #3
brenopop
Thread Starter
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
Quote:
Originally Posted by Red Menace View Post
Take a look at NSSecureTextField.
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

Last edited by brenopop; Jan 5, 2015 at 12:53 AM.
brenopop is offline   0 Reply With Quote
Old Jan 5, 2015, 12:53 AM   #4
subsonix
macrumors 68040
 
Join Date: Feb 2008
Quote:
Originally Posted by brenopop View Post
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 does't work, even with the curses library. Help
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.
subsonix is offline   1 Reply With Quote
Old Jan 5, 2015, 12:59 AM   #5
brenopop
Thread Starter
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
Quote:
Originally Posted by subsonix View Post
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.
Thank you, man. And how can i use curses? this initial setup and cleanup, How can I do it?
brenopop is offline   0 Reply With Quote
Old Jan 5, 2015, 01:04 AM   #6
subsonix
macrumors 68040
 
Join Date: Feb 2008
Quote:
Originally Posted by brenopop View Post
Thank you, man. And how can i use curses? this initial setup and cleanup, How can I do it?
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.
subsonix is offline   1 Reply With Quote
Old Jan 5, 2015, 01:05 AM   #7
brenopop
Thread Starter
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
Quote:
Originally Posted by subsonix View Post
Well, look up some tutorial on curses if you want to use that. I mean you can use noecho with curses, but it uses it's 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.
Yeah, that's what I want. Thank you so much.
brenopop is offline   0 Reply With Quote
Old Jan 5, 2015, 02:07 AM   #8
brenopop
Thread Starter
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
There's a problem now, I don't know how to use getpass() and how to make show up "*" instead the input of keyboard.
brenopop is offline   0 Reply With Quote
Old Jan 5, 2015, 07:59 AM   #9
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by brenopop View Post
There's a problem now, I don't know how to use getpass() and how to make show up "*" instead the input of keyboard.
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.
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 5, 2015, 11:15 AM   #10
Senor Cuete
macrumors regular
 
Join Date: Nov 2011
Quote:
Originally Posted by Red Menace View Post
Take a look at NSSecureTextField.
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.
Senor Cuete is offline   0 Reply With Quote
Old Jan 5, 2015, 01:47 PM   #11
brenopop
Thread Starter
macrumors newbie
 
Join Date: Jan 2015
Location: Brazil, Maranhão, São Luís
Send a message via Skype™ to brenopop
Quote:
Originally Posted by ArtOfWarfare View Post
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.
What can I do instead?
Isn't that the pattern for password in almost everything?

----------

Quote:
Originally Posted by Senor Cuete View Post
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.
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
brenopop is offline   0 Reply With Quote
Old Jan 5, 2015, 03:03 PM   #12
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by brenopop View Post
What can I do instead?
Isn't that the pattern for password in almost everything?
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.)
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 03:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC