Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

c/c++ passphrase entry

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
qleak
n00b
n00b
User avatar
Posts: 67
Joined: Fri Jun 17, 2005 7:35 pm

c/c++ passphrase entry

  • Quote

Post by qleak » Fri Aug 25, 2006 2:35 pm

I'm working on an experimental crypto program and want to have the user input a passphrase, but don't want it to be displayed.

I would like for there to be * characters or just a curser moving over with a blank.

I'm fairly sure I can do this with ncurses but this seems like using a bulldozer to crack a peanut.

Also when I use the endwin() command it clears the input (for example the following code
with g++ -lncurses

Code: Select all

#include <iostream>
#include <curses.h>

using namespace std;


int main() {
  string password;
  initscr();
  noecho();
  cout << "Enter Passphrase:\n";
  cin >> password;
  cout << "You will not see this\n";
  endwin();
  cout << "Passphrase entered successfully\n";
}

I could obviously use the getch() function inculded with ncurses to make the curser move or put in * characters
with a bit more work.


Anyone have any comments or other solutions for this?

Thanks in advance.

-Q
(\ /)
(O.o)
(> <)
This is Frank. He made me flood the school.
Top
dayul
Apprentice
Apprentice
User avatar
Posts: 180
Joined: Wed Jun 02, 2004 10:16 am
Location: Blackpool, England

  • Quote

Post by dayul » Fri Aug 25, 2006 3:29 pm

Without curses you can do something like this:

Code: Select all

#include <iostream>
#include <string>
#include <termios.h>

using namespace std;

int main()
{
	struct termios tstored, tnew;
	string password;

	cout << "enter password" << endl;

	/*store old settings and set new ones*/
	tcgetattr(0, &tstored);
	tnew= tstored;
	tnew.c_lflag&= (~ECHO);
	tcsetattr(0, TCSANOW, &tnew);

	cin >> password;

	cout << "password is " << password << endl;

	/*restore old settings*/
	tcsetattr(0, TCSANOW, &tstored);

	return(0);
}
Mailtc- system tray mail checker.
Top
dayul
Apprentice
Apprentice
User avatar
Posts: 180
Joined: Wed Jun 02, 2004 10:16 am
Location: Blackpool, England

  • Quote

Post by dayul » Fri Aug 25, 2006 3:34 pm

I also forgot to add that you can probably use the getpass() function, you would need to include unistd.h.
Both solutions probably depend on how portable you want it to be. Like you said, with ncurses getch() would probably be your best bet.
Mailtc- system tray mail checker.
Top
Genone
Retired Dev
Retired Dev
User avatar
Posts: 9656
Joined: Fri Mar 14, 2003 6:02 pm
Location: beyond the rim

  • Quote

Post by Genone » Fri Aug 25, 2006 3:36 pm

If you don't like the additional ncurses dep you can also control the echo behavior with the glibc terminal interface, just needs a bit more code (see `info libc terminal`)
Top
qleak
n00b
n00b
User avatar
Posts: 67
Joined: Fri Jun 17, 2005 7:35 pm

  • Quote

Post by qleak » Fri Aug 25, 2006 4:29 pm

Thanks both for you input. termios.h looks like a much simpler option that ncurses.

-Q
(\ /)
(O.o)
(> <)
This is Frank. He made me flood the school.
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic