Accepting Passwords from stdin

28 04 2008
I sometimes write small command line utilities that require a password. They are usually written in C++ for portability to other operating systems. Keep in mind that these snippets are only a "it-compiles-for-me" release, so don't hold me responsible for bugs you encounter.

In Xcode 3 (Mac OS X 10.5), I use the following code.
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>

static char * password;

int main (int argc, char * const argv[])
{
password = getpass("Enter Password: ");
printf("Password: %s\n", password);

return 0;
}


In Visual Studio 2003 and Bloodshed Dev-C++ (Windows XP SP2), I use the following code.
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

using namespace std;

char * promptForPassword();

int main(int argc, char *argv[])
{
// Get and display the password
char * pw = promptForPassword();
printf("Password: %s\n", pw);
delete [] pw;

return 0;
}

char * promptForPassword()
{
char * pw = new char[1024]; // allow for password to have a maximum size of a kilobyte (could be handled dynamically by mallocing, strcpyring, freeing)
int i = 0;
char ch;

// Display the prompt
printf("Enter Password: ");

// Handle input
do
{
_flushall();
fflush(stdin);

ch = _getch();
switch (ch)
{
// If new character is the return key, just ignore and end the loop
case '\r':
break;

// If new character is the null char, ignore it
case '\0':
break;

// If new character is the backspace key, clean up the stdout and pw string
case '\010':
if (i > 0 && i <=1024)
{
cout<<"\010 \010"; // push the input marker back one character, overwrite the last character with a space, push input marker back
i--;
pw[i] = '\0';
}
break;

// Otherwise, append character to the string where possible
default:
if (i >= 0 && i < 1024)
{
pw[i] = ch;
cout<<'*';
i++;
}
break;
}
} while(ch != '\r');
pw[i] = '\0';

// After the user hits enter to end the password prompt, display a new line
printf("\n");

return pw;
}


My promptForPassword() function displays asterisk characters instead of input.

Of course, it is possible to combine the getpass() function from pwd.h into promptForPassword() by using #ifdef directives, so that the code would compile in all three IDEs (and all three compilers: Xcode uses gcc, Visual Studio uses Microsoft's compiler (I forget what they call it), and Dev-C++ uses mingw).


Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)
11 10 2010
#1 Louis Vuitton (Reply)

Of course, it is possible to combine the getpass() function from pwd.h into promptForPassword() by using #ifdef directives, so that the code would compile in all three IDEs
23 11 2010
#2 FitzgeraldImelda28 (Reply)

There're a lot of complications on the career constructing way. But, the written essay service was invented to help university students with custom research papers composing.
15 05 2011
#3 buy an essay (Reply)

Our service attempt to aid college students in the world. Hence, we give them a chance to buy an essay for reasonable prices.
16 07 2011
#4 Harold (Reply)

I just wanted to leave a quick comment to say that your blog was nice. I found it on google search after going through a lot of other information that was not really relevant. I thought I would find this much earlier considering how good the information is. =-=
12 08 2011
#5 Premium Audit (Reply)

Okay, thank you for the updates, totally believe in your things, I am excited to try some of the things!!
Chris Harris
12 01 2012
#6 custom thesis (Reply)

Thank you a lot for the really hot information close to this topic. Can you aid to find the thesis service or an experienced dissertations writing service to order the legal dissertation in?

Add Comment


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA