password input

Pages: 12
May 13, 2012 at 5:25pm
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 May 17, 2012 at 3:04pm
May 13, 2012 at 5:50pm
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!
May 13, 2012 at 5:52pm
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.
May 13, 2012 at 5:56pm
I don't really understand that code you've sent me :/
May 14, 2012 at 6:42am
pls help me someone :(
May 14, 2012 at 8:23am
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" ;
}



May 14, 2012 at 11:46am
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 :/
May 14, 2012 at 12:28pm
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 May 14, 2012 at 12:29pm
May 15, 2012 at 5:32pm
I don't know :/ I'm new at this :/
May 15, 2012 at 7:57pm
Did you forget to link with -lcurses ?
May 17, 2012 at 2:48pm
I don't know what that is... :/
Pls help me
May 17, 2012 at 3:11pm
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.

May 17, 2012 at 3:24pm
error: 'nullptr' was not declared in this scope
:/
May 17, 2012 at 3:27pm
error: 'nullptr' was not declared in this scope


Time to get a modern compiler :)
May 17, 2012 at 3:27pm
nullptr is only available on new compilers. Use 0 or NULL instead.
May 17, 2012 at 3:27pm
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? ... .
May 17, 2012 at 3:38pm
well, no error now, but It didn't asked me to enter anything :/ just typed: error reading pass phrase
May 17, 2012 at 4:02pm
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.

May 17, 2012 at 4:23pm
I don't have command line :) I use Eclipse :)
May 17, 2012 at 4:27pm
I guarantee you that you have a command line.
Pages: 12