Masking password input

This is a discussion on Masking password input within the C++ Programming forums, part of the General Programming Boards category; You know when you enter a password and it's masked by '*'s? I've been trying to implement that, but with ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Masking password input

    You know when you enter a password and it's masked by '*'s? I've been trying to implement that, but with no luck. Here's an example:

    Code:
    int main() 
    {
    	char* in_pwd = (char*) calloc(20, sizeof (char));
     
    	cout << "Password: ";
     
    	// enter solution here :D
     
    	// compare etc
     
    	return 0;
    }
    Please don't moan about my use of char* and casts, that's not my problem (yet lol).

    I figure I need to output a backspace then a '*' for each character input?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    backspace in ASCII is 8:
    char(8);

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that doesn't work because you're putting the character into the console... and you could just use '\b' if you wanted a backspace

    you have to catch the keystroke before it hits the cosole, and while there's no standard way to do it (AFAIK), you might want to look into the function getch();

    FAQ > How do I... (Level 2) > How can I get input without having the user hit [Enter]?
    Last edited by major_small; 07-18-2005 at 11:51 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Never mind I've solved it
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    Well, here's my "conio.h" version for all to enjoy anyways...
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    using namespace std;
    
    #include <conio.h>
    
    bool get_password(string &password,
                      char echo_char = '*',    // echo character
                      bool esc_breaks = true,  // does hitting ESC return false?
                      size_t limit_length = 0) // is there a length limit?
    {
        const int ESC = 27;
        const int BACKSPACE = 8;
    
        int c;
        vector<char> input;
        input.reserve(10);
    
        // clear password
        password.erase(password.begin(), password.end());
    
        for (;;)
        {
            c = getch();
    
            if (c == '\r')
            {
                input.push_back(0); // C-string terminator
                password = &input[0];
                break;
            }//if
    
            // only accept printable chars
            if (isprint(c))
            {
                if ((limit_length == 0) ||
                    ((limit_length != 0) && (input.size() < limit_length)))
                {
                    input.push_back(c);
                    cout.put(echo_char);
                }//if
                else
                {
                    cerr.put('\a');
                }//else
            }//if
    
            // support for backspace functionality
            if (c == BACKSPACE)
            {
                if (!input.empty())
                {
                    cout.put(c);   // backspace
                    cout.put(' '); // overwrite existing char
                    cout.put(c);   // backspace back into position
                    input.pop_back();
                }//if
                else
                {
                    cerr.put('\a');
                }//else
            }//if
    
            if (esc_breaks && (c == ESC))
            {
                break;
            }//if
        }//for(;;)
    
        return !password.empty();
    }//get_password
    
    int main()
    {
        string password;
    
        cout << "Enter Password: " << flush;
    
        if (get_password(password))
            cout << "\nYou entered: " << password << endl;
        else
            cout << "\nNo password entered." << endl;
    
        return 0;
    }//main
    gg

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Do I have permission to use this in my program(s)?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    you have permission to use my program. lol

    Code:
    #include <conio.h> 
    #include <ctype.h> 
    #include <iostream>
    
    using namespace std;
    int main()
    {int l0l0lll0l0l0l;char l0l0lll0l[101];
     int l0l0lll0l0l = 0;
     cout<<"Enter your password>>";
     while ((l0l0lll0l0l0l = getch()) != EOF 
     && l0l0lll0l0l0l != '\n' && l0l0lll0l0l0l != '\r' 
     && l0l0lll0l0l < sizeof(l0l0lll0l) - 1)
    {if (l0l0lll0l0l0l == '\b' && l0l0lll0l0l > 0) 
    {printf("\b \b");fflush(stdout);
    l0l0lll0l0l--;l0l0lll0l[l0l0lll0l0l] = '\0';}
    else if (isalnum(l0l0lll0l0l0l)){cout<<"*";
    l0l0lll0l[l0l0lll0l0l++] = (char)l0l0lll0l0l0l;}}
    l0l0lll0l[l0l0lll0l0l] = '\0';cout<<""<<endl;
    cout<<"You entered  <"<<l0l0lll0l<<">";
    int l00l0ll0;cin>>l00l0ll0;return 0;}
    This was just taken from FAQ anyway, Majorsmall's link

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    >> Do I have permission to use this in my program(s)?
    Sure.

    gg

  10. #10
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by ahluka
    Code:
    int main() 
    {
    	char* in_pwd = (char*) calloc(20, sizeof (char));
     
    	cout << "Password: ";
     
    	return 0;
    }
    you need not to cast calloc, see the faq
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with inputting password?
    By Abda92 in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 01:14 PM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02:42 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Masking user input in C
    By alpha561 in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 02:18 PM
  5. Hiding input password
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 07:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21