password input

Pages: 12
Hi :)
I was trying to figure out, how to mask password input with '*'. And I found this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio.h>

using namespace std;
int main(){
   string pass;
   char ch;
   cout << "Enter pass\n";
   ch = _getch();
   while(ch != 13){
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
   if(pass == "mypass"){
      cout << "\nAccess granted :P\n";
   }else{
      cout << "\nAccess aborted...\n";
   }
}

since I'm Mac user, that's not really for me, so I tried ncurses.h with getch() and get weird error, well, weird for me as a beginner:
1
2
3
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [password] Error 1

What can I do with this to make it run.

and btw, I'm using Eclipse IDE for C/C++ Developers for Mac, if it helps :)
Last edited on
I don't know why people want asterisks for passwords -- you are better off not printing anything while getting a password. But if you insist, check out these posts:

http://www.cplusplus.com/forum/general/3570/#msg15410
http://www.cplusplus.com/forum/general/3570/#msg15410

They were written for Microsoft Windows, but you can translate the ideas in them easily enough:

GetStdHandle() - safe to ignore
(Get|Set)ConsoleMode() - safe to ignore
ReadConsoleA() - getch() / _getch()
WriteConsoleA() - putchar()

Good luck!
I don't insist :) It's just native to see password as asterisks :P
I work in linux too, so I'm used to "see" nothing as pass too :P
I'll check those you sent me.
I don't really understand that code you've sent me :/
pls help me someone :(
Mac OS X is FreeBSD; you can use either getpass() or readpassphrase(). These won't echo anything, though.

http://www.freebsd.org/cgi/man.cgi?query=readpassphrase&sektion=3&apropos=0&manpath=FreeBSD+9.0-RELEASE

Something like:
1
2
3
4
5
6
7
8
9
10
#include <readpassphrase.h>
#include <string>

std::string get_passphrase( const std::string& prompt )
{
    char buffer[2048] ;
    char* phrase = readpassphrase( prompt.c_str(), buffer, sizeof(buffer),
                                   RPP_REQUIRE_TTY ) ;
    return phrase != nullptr ? phrase : "error reading pass phrase" ;
}



Tried, but still the same error:

1
2
3
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [password] Error 1


and I don't really understand that code you sent :/
closed account (1vRz3TCk)
It looks like the linker can't find something that it needs...is there any output prior to the 'ld: symbol(s) not found' output?
Last edited on
I don't know :/ I'm new at this :/
Did you forget to link with -lcurses ?
I don't know what that is... :/
Pls help me
Create a simple "hello world" style console application with just this much:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <readpassphrase.h>
#include <string>
#include <iostream>

std::string get_passphrase( const std::string& prompt )
{
    char buffer[2048] ;
    char* phrase = readpassphrase( prompt.c_str(), buffer, sizeof(buffer),
                                   RPP_REQUIRE_TTY ) ;
    return phrase != nullptr ? phrase : "error reading pass phrase" ;
} 

int main() { std::cout << get_passphrase( "enter a phrase: " ) << '\n ; } 


Or just type it in using a text editor, and compile and run it from the command line.

error: 'nullptr' was not declared in this scope
:/
error: 'nullptr' was not declared in this scope


Time to get a modern compiler :)
nullptr is only available on new compilers. Use 0 or NULL instead.
nullptr is only available in C++11. It is generally better to test pointers by making use of the implicit conversion to bool anyway, so shorten it to phrase? ... .
well, no error now, but It didn't asked me to enter anything :/ just typed: error reading pass phrase
a. Run the program as it is from the command line.

b. Remove the forced read only frrom/dev/tty (ignore stdin) option - replace RPP_REQUIRE_TTY with 0

And tell us what happens.

I don't have command line :) I use Eclipse :)
I guarantee you that you have a command line.
Pages: 12