Password program

I'm looking to write a small, basic password program and was wondering if someone could tell me some of the headers and functions I might need.

Then I'll just see how i get on :)

Thanks.
What aspect do you mean? Input without echo, checking or storage? How secure should it be?
To start with, just the basic input without echo (which i think is getch()?), being able to delete characters and the storage they are kept in (vector maybe?) and i'll doing checking and what not myself.

Btw this won't be made to send passwords to a file or anything like that, its mainly the actual input i'm interested in at the moment.
And how secure should it be?

If you don't care too much about security, you can just use getch() to read characters into a buffer to make up a string password. Then you can compare that to some held password somewhere with strcmp().

How much you care about security determines how much more complicated it is. For example, if you don't want your password showing up in a core dump, you want to hold an encrypted version, which means encrypting what's read and comparing those instead. And so on.
Ok thanks. Will post back with some code at some point!
Ok I got stuck, i'm not looking for a solution though, just an indication...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <conio.h>
#include <vector>

int main()
{
    std::vector<char> pass(20);
    char input = getch();
    int i = 0;
    
    while(input != '\n') 
      {
        if((i==0) && (input == '\b')) continue;
        if(input == '\b')  
          {     
            pass.pop_back();
            std::cout << "in if"; // debugging purposes
            std::cout << '\b';
            i--;
          }
        else
          {
            pass.at(i) = input;
            i++;
            std::cout << '*';
          }
             
        getch();    
      }
    
    std::cin.get();
    return 0;
}


The backspace doesn't work, and nor does enter :S
Why are you using .at() (line 23)? Just use .push_back() to add the extra character.

Your getch() on line 28 isn't updating input.
Sorry, first time using vectors...

Here's the new code which has some odd lines which are just used for debugging:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <conio.h>
#include <vector>

int main()
{
    std::vector<char> pass(20);
    char input = getch();
    int i = 0;
    
    while(input != (char)13) 
      {
        if((i==0) && (input == '\b')) continue;
        if(input == '\b')  
          {     
            pass.pop_back();
            std::cout << '\b';
            i--;
          }
        else
          {
            pass.push_back(input);
            i++;
            std::cout << '*';
          }

        if(i==5) break;  // debug
        input = getch();  
      }
      
    std::cout << '\n'; // debug
    for(int i=0;i<pass.size();i++)    // debug
      std::cout << pass.at(i); 
      
    std::cin.get();
    return 0;
}


So it does work apart from when i press backspace, the last star doesn't get deleted. However, the right characters are in the vector.

And if the first input is a backspace it just doesn't do anything :(
Last edited on
How come it only lets you delete all the way until there's 5 characters still left?
This version doesn't have a limit like the other one either.
Last edited on
Yeah I've seen that before but was hoping to do it without it. Can you tell me two things though plese:

what does the ~ mean in the code ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)?

And can you give me an explanation of what the input buffer is?
The ~ gives a bitwise inverse of the given value. For example, a byte holds eight bits.

00000010
This is the bit pattern for the number '2'

invert it

11111101
This is the bit pattern for the number '253' (unsigned) or '-3' (signed, two's complement)

This is useful when manipulating bit masks.
For example, suppose you want to turn off bit 2 (the third bit) in the values

00101100
Bit two is set
00101000
Bit two is clear
The code to do so is the same no matter what the current state of the bit to clear:
value = value & ~(0x04);

So, in the code you indicated, I am removing the bits for 'echo' and 'line-buffered' input from the input mode mask. That means that input characters do not echo to the output display, nor must the user press the ENTER key before the input is sent to the program -- each key press is immediately available to the program.


The input buffer is nothing more than a string that caches input to your program.


Hope this helps.

[edit]
BTW, in response to the star question... The '\b' character only causes the cursor to back up one character. It doesn't remove any previous input. That's why in my password function the output sequence is "\b \b" -- back up, print a space, and back up again.
Last edited on
BTW, in response to the star question... The '\b' character only causes the cursor to back up one character. It doesn't remove any previous input. That's why in my password function the output sequence is "\b \b" -- back up, print a space, and back up again.

You literally read my mind! I was going to post that until I saw your code "\b \b" I misunderstood what the "\b" actually did. When tutorials say its a backspace I thought it meant the type of backspace that would occur if you was using a word processor e.g. you would go back one space and remove anything that was occupying that space. But now I realise it doesn't, it literally means go back one space!

Thanks for he reply ^^
Topic archived. No new replies allowed.